Rumah > pangkalan data > tutorial mysql > linux c mysql编程_MySQL

linux c mysql编程_MySQL

WBOY
Lepaskan: 2016-06-01 13:11:45
asal
1193 orang telah melayarinya

登录不了: 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 <stdlib.h>#include <stdio.h>#include <string.h>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);}
Salin selepas log masuk




sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan