c连接mysql老是报错

c连接mysql老是报错,第1张

链接错误, mysql的 库没有加进来。你找一下 mysql的lib库。

在main函数前面加:

#pragma comment(lib, "mysql.lib")

上面的名字 "mysql.lib" 换成你lib库的名字。 然后把这个库放到程序 目录

给你个例子:

这个是DEV C++编译通过的

#include <windows.h>

#include <iostream>

#include <mysql/mysql.h>

#define SELECT_QUERY "select * from stu "

using namespace std

int main(int argc, char *argv[])

{

//connection params

char *host = "localhost"

char *user = "root"

char *pass = "123"

char *db = "mydb"

//sock

MYSQL *sock

MYSQL_RES *res

sock = mysql_init(0)

if (sock) cout <<"sock handle ok!" <<endl

else{

cout <<"sock handle failed!" <<endl

return EXIT_FAILURE

}

//connection

if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))

cout <<"connection ok!" <<endl

else{

cout <<"connection failed!" <<endl

return EXIT_FAILURE

}

//query

if (mysql_query (sock, SELECT_QUERY ))

{

cout <<"Query failed " <<mysql_error(sock) <<endl

exit(1)

}

if ( !( res = mysql_store_result( sock )))

{

cout <<"Couldn't get result from " <<mysql_error(sock) <<endl

exit(1)

}

MYSQL_FIELD *field

MYSQL_ROW row

while ((field = mysql_fetch_field(res)))

{

printf("field name %s ", field->name)

}

while ( row = mysql_fetch_row ( res ))

{

cout<<row[0]<<" "<<row[1]<<endl

}

system("PAUSE")

//closing connection

mysql_free_result ( res )

mysql_close(sock)

return EXIT_SUCCESS

}

执行 ./bin/mysqld --defaults-file=xxx --initialize-insecure 初始化data目录

./bin/mysqld_safe --defaults-file=/home/xxx/mysql/my.cnf &

运行后netstat -anlp | grep mysql 发现mysqld进程使用的是unix domain socket,没使用tcp/tcp6,没监听端口,看配置文件里面有一行 --skip-grant-tables,看官方文档,如果开启了--skip-grant-tables默认启用--skip-networking,这样就不允许远程连接了,因为--skip-grant-tables不安全。

https://www.oreilly.com/library/view/mysql-8-cookbook/9781788395809/6ea03335-6ff2-4d4f-a008-48c8cf88fd01.xhtml#:~:text=In%20this%20method%2C%20you%20stop,to%20connect%20to%20the%20server .

./bin/mysqladmin shutdown


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7440113.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-05
下一篇 2023-04-05

发表评论

登录后才能评论

评论列表(0条)

保存