class-notes/课件/C语言/C语言连接MySQL/VC2010连接MySQL配置.md
2025-02-06 22:43:19 +08:00

116 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 1.创建项目
<img src="https://yp.smallkun.cn/markdown/image-20241222165137489.png!compress" alt="image-20241222165137489" style="zoom:50%;" />
创建项目时选择空项目
### 2.配置输出控制台
<img src="https://yp.smallkun.cn/markdown/image-20241222165231278.png!compress" alt="image-20241222165231278" style="zoom:50%;" />
右键项目名称选择`属性`
配置属性->链接器->系统->子系统设置为`控制台 (/SUBSYSTEM:CONSOLE)`
<img src="https://yp.smallkun.cn/markdown/image-20241222165309647.png!compress" alt="image-20241222165309647" style="zoom:50%;" />
### 3.安装连接软件
- 64位编译器选择**Windows (x86, 64-bit), ZIP Archive**(mysql-connector-c-6.1.11-winx64.zip)
- 32位编译器选择**Windows (x86, 32-bit), ZIP Archive**(mysql-connector-c-6.1.11-win32.zip)
https://downloads.mysql.com/archives/c-c/?version=6.1.11&amp;os=src
![image-20240518184737189](https://yp.smallkun.cn/markdown/image-20240518184737189.png!compress!compress)
安装完后会在C:\Program Files (x86)\MySQL\MySQL Connector C 6.1目录下
### 4.配置头文件和lib文件
**配置头文件路径**
C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include
右键项目名称点击属性 找到 配置属性下的VC++目录 将上述路径加入到`包含目录`
<img src="https://yp.smallkun.cn/markdown/image-20241222170322231.png!compress" alt="image-20241222170322231" style="zoom:50%;" />
点击最右侧下拉框后点击编辑
![image-20241222170447324](https://yp.smallkun.cn/markdown/image-20241222170447324.png!compress)
**配置lib文件路径**
C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib
右键项目名称点击属性 找到 配置属性->链接器->常规 将上述路径加入到`附加库目录`
<img src="https://yp.smallkun.cn/markdown/image-20241222170720851.png!compress" alt="image-20241222170720851" style="zoom:50%;" />
**配置附加依赖性**
配置属性——>链接器——>输入——>附加依赖项
<img src="https://yp.smallkun.cn/markdown/image-20241222170820644.png!compress" alt="image-20241222170820644" style="zoom:50%;" />
在其中加入`libmysql.lib`
<img src="https://yp.smallkun.cn/markdown/image-20241222171057161.png!compress" alt="image-20241222171057161" style="zoom:50%;" />
### 5.编写数据库连接代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
void init_conn(MYSQL* mysql) {
mysql_init(mysql); // 初始化 MySQL 连接
if (!mysql_real_connect(mysql, "localhost", "root", "root", "user", 3306, NULL, CLIENT_IGNORE_SIGPIPE)) {
printf("连接数据库时发生错误!\n");
printf("%s\n", mysql_error(mysql));
}else{
printf("连接成功!\n");
}
}
int main(void){
MYSQL mysql;
int ret = 0;
init_conn(&mysql);
return 0;
}
```
### 6.配置my.ini
运行代码后会发现
<img src="https://yp.smallkun.cn/markdown/image-20240518190612777.png!compress!compress" alt="image-20240518190612777" style="zoom:50%;" />
要永久地关闭 MySQL 中的 SSL 连接功能,需要进行以下步骤:
编辑 MySQL 配置文件 my.cnf或 my.ini一般位于 MySQL 安装目录的 /etc 或 /etc/mysql 子目录下。
并添加以下配置:
Windows下此文件路径在C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
```ini
ssl=0
```
1. 保存文件并退出编辑器。
2. 重启 MySQL 服务。
**测试连接**
![image-20241222171136732](https://yp.smallkun.cn/markdown/image-20241222171136732.png!compress)