ubuntu下执行c文件找不到mysql.h,dev已装 如图,whereis mysql.h显示存在,mysql服务也开了..

您所在的位置:网站首页 找不到hosts文件 ubuntu下执行c文件找不到mysql.h,dev已装 如图,whereis mysql.h显示存在,mysql服务也开了..

ubuntu下执行c文件找不到mysql.h,dev已装 如图,whereis mysql.h显示存在,mysql服务也开了..

2023-04-07 21:20| 来源: 网络整理| 查看: 265

今天看着看着代码,突然就想学学linux下c中怎么连接mysql数据库的。带着好奇的心理编写了,最最简单的连接程序:

#include stdio.h

#include mysql.h

MYSQL *mysql//首先要定义个mysql对象,来实现初始化。

main (void){

mysql_init(mysql)//初始化mysql数据库

if(mysql_real_connect(mysql,"localhost","root","123qwe","test",3306,0,0)){//此方法是用于连接mysql数据库的,详细参数请参照:http://dev.mysql.com/doc/refman/5.1/zh/apis.html#mysql-real-connect

printf("OK\n")//成功,输出OK

return 0

}

else {

printf("ERORR\n")//失败输出ERORR

}

}

简单编译下:gcc -0 test_sql test_sql.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient

悲催的来了:test_sql.c:2: fatal error: mysql.h: 没有那个文件或目录.

居然告诉我说没有mysql.h这个文件,可是实验了,我确实安装了mysql了啊。。。。。。。

我想可能是缺少lib库了吧,百度啊,google啊,一顿搜,哦!~

原来是缺少libmysqlclient-dev,OK安装就是了,apt-get install libmysqlclient-dev

继续编译,结果:

test_sql.c: In function ‘main’:

test_sql.c:7: warning: passing argument 1 of ‘mysql_init’ from incompatible pointer type

/usr/include/mysql/mysql.h:434: note: expected ‘struct MYSQL *’ but argument is of type ‘struct MYSQL **’

test_sql.c:8: warning: passing argument 1 of ‘mysql_real_connect’ from incompatible pointer type

/usr/include/mysql/mysql.h:441: note: expected ‘struct MYSQL *’ but argument is of type ‘struct MYSQL **’

开放远程访问

开启root用户的全称访问权限

1.修改数据库中user的host

sh

# 使用mysql -u root -p登录到数据库,然后依次执行下面语句

# xxxxxx表示root用户的密码

use mysql

update user set host = '%' where user ='root'

grant all privileges on *.* to 'root'@'%' identified by 'xxxxxx'

flush privileges

2.修改my.conf的中的ip绑定

sh

# 进入编辑/etc/mysql/mysql.conf.d/mysqld.conf

vi /etc/mysql/mysql.conf.d/mysqld.conf

# 修改ip绑定

# 源文件中为:

bind-address 127.0.0.1

# 将其修改为:

bind-address 0.0.0.0

# 覆盖保存

esc:wq

3.重启数据库

sh

# 重启命令

service mysql restart

新增用户并允许远程访问

# 新增用户并允许远程访问只需要在user表中增加一个用户,将host设置为%即可

# 下例默认将所有权限分配给新用户,例如:

grant all privileges on *.* to 'lethew'@'%' identified by 'abcdef'

flush privileges

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

原文地址:https://outofmemory.cn/zaji/7304779.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3