Ubuntu 上 C 和 MySql 中的访问拒绝错误
你好 我正在尝试在我的项目中使用C连接MySql,这是我第一次使用C和MySql。 我尝试在 MySql 中保存用户名、密码和角色。当我这样运行程序时,一切正常。
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
char userName[45] ="hi";
char userPassword[45];
char role[45];
strcpy(userName,"koko");
strcpy(userPassword,"hi");
strcpy(role,"admin");
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",userName,userPassword,role);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
尽管有一些警告,但我可以毫无错误地运行它。 但是当我使用函数编写这个程序时,我在编译时遇到了这个错误。 用户“root”@“localhost”的访问被拒绝(使用密码:YES)
#include <mysql.h>
#include <stdio.h>
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
void saveUser(char * , char * ,char * );
void updateUser(char * , char * ,char * );
void showData();
main()
{
saveUser("kevin","hi","admin");
}
void saveUser(char *name, char *password,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",name,password,role);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
res = mysql_use_result(conn);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
void updateUser(char *name, char *password,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
printf("1234511\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"update userTbl set password=\'%s\',role=\'%s\' where name=\'%s\'; ",password,role,name);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
void showData()
{
/* output table name */
system("clear");
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
}
任何人都可以解释我哪里出了问题以及如何纠正它。 我使用 gcc 获取可执行文件并在命令提示符下运行它。 提前致谢。
凯文
Hi
I'm trying to connect MySql using C in my project and it's the first time I use C and MySql.
I try to save a user name, password and role in MySql. When I run the program like that it's ok.
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
char userName[45] ="hi";
char userPassword[45];
char role[45];
strcpy(userName,"koko");
strcpy(userPassword,"hi");
strcpy(role,"admin");
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",userName,userPassword,role);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
I can run it without any errors though there are some warnings.
But when i wrote this program by using function, I got this error when I compile it .
Access denied for user 'root'@'localhost' (using password: YES)
#include <mysql.h>
#include <stdio.h>
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
void saveUser(char * , char * ,char * );
void updateUser(char * , char * ,char * );
void showData();
main()
{
saveUser("kevin","hi","admin");
}
void saveUser(char *name, char *password,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",name,password,role);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
res = mysql_use_result(conn);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
void updateUser(char *name, char *password,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
printf("1234511\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"update userTbl set password=\'%s\',role=\'%s\' where name=\'%s\'; ",password,role,name);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
void showData()
{
/* output table name */
system("clear");
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
}
Can anybody explain me where I went wrong and how can I correct it.
I use gcc to get the executable file and run it on command prompt.
Thanks in advance.
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我读了你的帖子,这不是你的代码的问题尝试使用mysql客户端软件(如mysql查询浏览器或sql yog或phpmyadmin)使用参数(用户名和密码)连接到mysql服务器。
我认为您将密码设置为空白,因此只需替换该行
即可
确保您以正确的大小写传递参数,因为 ubuntu 的行为区分大小写,
如果有帮助,请尝试一下。
请检查您的用户名和密码是否正确。通常,当您安装 mysql 服务器时,它会要求设置密码和用户。当您使用 ubuntu 时,您可能正在使用 lamp 服务器,因此请尝试获取正确的凭据
,只有问题出在您的凭据上
i read your post it's not the problem with your code try to connect to mysql server with the parameters(username and password) using a mysql client software like mysql query browser or sql yog or phpmyadmin whatever.
i think you set your password to blank, so simply replace the line
to
also ensure that you are passing the parameters in correct case since ubuntu's behavior is case senstive
give it a try if it helps.
please check for your correct username and passwords. usually when you install mysql server it asks for password and user to be set. as you are using ubuntu you might be using lamp server so try to get the correct credentials
there only the problem is with your credentials
Simon 和 Devjosh 您好,感谢您的帮助。我发现了问题。_全局变量名密码与saveUser函数中的参数名密码相同_。非常感谢,很抱歉打扰您。
凯文
Hi both Simon and Devjosh , Thanks for you help. I found the problem._The global variable name password is the same with the parameter name password in saveUser function _. Thanks a lot and so sorry disturb you .
Kevin