- 首先本机安装mysql,登录数据库:
mysql -uroot -p
- 开启远程访问权限:
grant all privileges on *.* to root@'%' identified by "password";
这里是把本机所有的数据库共享给用户名为root 密码是:password
3.登录虚拟机,安装mysql,写入代码测试:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<mysql/mysql.h>
int main()
{
MYSQL mysql;
if (NULL == mysql_init(&mysql)) {
printf("mysql_init(): %s\n", mysql_error(&mysql));
return -1;
}
if (NULL == mysql_real_connect(&mysql,
"192.168.100.24", //ip or host
"root", // name
"root", //password
"information_schema", //databases name
3306, //port
NULL,
0)) {
printf(