欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

linux c mysql编程_MySQL

程序员文章站 2024-01-23 14:20:52
...
登录不了: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
1./etc/rc.d/init.d/mysqld status 看看mysql是否已经启动
service mysql start启动mysql :mysql: unrecognized service 改为:/etc/rc.d/init.d/mysqld start 启动成功
2.mysql --version或登录执行select version(); :5.1.66
3.Navicat登录 执行: GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.8.123' IDENTIFIED BY '' WITH GRANT OPTION; FLUSH PRIVILEGES; 允许用户root从ip为192.168.8.123的主机连接到mysql服务器
4.写DEMO,mysql_test.c:1:19: error: mysql.h: No such file or directory 配置环境变量: /usr/include/mysql /usr/lib64/mysql
5.程序连接数据库时: Host 'ucrcserver1' is not allowed to connect to this MySQL server : GRANT ALL PRIVILEGES ON *.* TO 'zangzy'@'' IDENTIFIED BY '' WITH GRANT OPTION; FLUSH PRIVILEGES;
6.我写的小DEMO 编译: gcc mysql_test.c -L/usr/lib64/mysql -lmysqlclient -lz
#include "/usr/include/mysql/mysql.h"#include #include #include int main(void){	printf("Hello MYSQL./n");	char sql[4000];	sprintf(sql, "INSERT INTO `km_login_info`(`KmNumber`,`UserPwd`,`IfOnline`,`LoginIP`,`LoginTime`,`FriendsCount`,`IfMsg`) VALUES('95215710','123','0','192.168.8.123','2012-12-25',100,'1')");	const char *g_host_name = "192.168.8.5";	const char *g_user_name = "zangzy";	const char *g_password = "123";	const char *g_db_name = "im";	const unsigned int g_db_port = 3306;	MYSQL *g_conn;	//mysql连接		MYSQL_RES *g_res;	//mysql记录集	MYSQL_ROW g_row;	//mysql记录行	g_conn = mysql_init(NULL);	if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) {		puts(mysql_error(g_conn));		exit(-1);	}		//if (mysql_real_query(g_conn, sql, strlen(sql)))	//	puts(mysql_error(g_conn));	memset(sql, 0x00, sizeof(sql));		strcpy(sql, "select * from `km_login_info`");	if (mysql_real_query(g_conn, sql, strlen(sql)))		puts(mysql_error(g_conn));		g_res = mysql_store_result(g_conn); //从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录		//int iNum_rows = mysql_num_rows(g_res); //得到记录的行数	//int iNum_fields = mysql_num_fields(g_res); //得到记录的列数		while ((g_row=mysql_fetch_row(g_res))) //打印结果集		printf("%s/t%s/n", g_row[0], g_row[1]); //第一,第二字段	mysql_free_result(g_res); // 释放结果集	mysql_close(g_conn);	//关闭链接	exit(0);}