一、下載mysql8.rar解壓到C盤(也可以解壓到其他位置)
在系統環境變量添加JAVA_HOME=C:\myslq8,并在path中添加%JAVA_HOME%\bin;
二、以管理員身份進入命令窗口
三、修改配置文件指定安裝路徑和數據庫的存放路徑
四、鍵入如下命令初始化并啟動mysql服務,然后修改登錄密碼
C:\>mysqld --initialize --user=mysql --console
C:\>mysqld -install
C:\>net start MySQL
C:\>mysql -u root -p (回車讓輸入的密碼就是第一個命令后出現的臨時密碼)
mysql> ALTER USER root@localhost IDENTIFIED BY "12345678"; (新修改密碼自己指定)
mysql> flush privileges;
mysql> exit
五、手動命令建數據庫的命令如下
C:\> mysql -u root -p
Enter password: ******** (上面自己修改的新密碼)
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create testdatabase; (創建的數據庫為testdatabase)
mysql> use testdatabase;? (切換到剛建的數據庫)
mysql> create table customers (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(255),address VARCHAR(255),city VARCHAR(255)); (創建表customers)
mysql> insert into customers (name,address,city) values('張三','海淀區','北京'); (插入新記錄)
mysql> select * from customers; (顯示新記錄)
+----+--------+-----------+--------+
| id | name?? | address?? | city?? |
+----+--------+-----------+--------+
|? 1 | 張三?? | 海淀區??? | 北京?? |
+----+--------+-----------+--------+
1 row in set (0.00 sec)
六、VS2022調試讀取源代碼如下
// TestMySQL.cpp : 此文件包含 "main" 函數。程序執行將在此處開始并結束。
//#include <stdio.h>
#include <iostream>
#include "include/mysql.h"using namespace std;
#pragma comment(lib,"lib/libmysql.lib")int main()
{MYSQL mysql;mysql_init(&mysql);if (!(mysql_real_connect(&mysql, "127.0.0.1", "root", "12345678", "testdatabase", 0, NULL, 0))){cout << "Connect database,fail!\n : ";cout << mysql_error(&mysql);exit(-1);}mysql_query(&mysql, "set names gbk");mysql_query(&mysql, "select * from customers where name = '張三'");MYSQL_RES* res = mysql_store_result(&mysql);if (res == NULL){mysql_close(&mysql);cout << mysql_error(&mysql);exit(-1);}MYSQL_ROW row;int count = mysql_num_fields(res);while (row = mysql_fetch_row(res)){for (int kkk = 0; kkk < count; kkk++){cout << row[kkk] << "\t";}cout << endl;}mysql_free_result(res);mysql_close(&mysql);return 0;
}
七、編譯時前要把mysql依賴庫和包含文件拷貝到工程下
?八、編譯后的運行結果如下
?