這里寫自定義目錄標題
- 下載MySQL ZIP壓縮包
- 安裝主庫
- 1、創建配置文件
- 2、安裝服務
- 3、初始化數據庫
- 4、啟動服務
- 5、配置主庫
- 安裝從庫
- 1、配置`ini`文件
- 2、安裝服務
- 3、初始化數據庫
- 4、啟動服務
- 5、配置從庫
- 6、驗證從庫狀態
- 操作主庫驗證
下載MySQL ZIP壓縮包
https://dev.mysql.com/downloads/mysql/
安裝主庫
直接解壓就行,我的安裝位置是 D:\module\mysql801
,
1、創建配置文件
在解壓根路徑創建my.ini
配置文件
[mysqld]
port=18001 # 主庫端口
basedir=D:\module\mysql801 # 安裝路徑
datadir=D:\module\mysql801\data # 數據文件夾
server-id=1 # id,與從庫不同
log-bin=mysql-bin # 開啟binlog
binlog_format=ROW
max_connections=200
default_authentication_plugin=mysql_native_password
2、安裝服務
在根目錄(my.ini
所在的目錄)使用管理員權限打開cmd
或者powershell
# 注意mysqld是主庫安裝目錄下bin文件里的程序: "D:\module\mysql801\bin\mysqld.exe"
.\bin\mysqld.exe --install MySQL8_Master --defaults-file="D:\module\mysql801\my.ini"
3、初始化數據庫
# --initialize-insecure 是不設置root密碼,mysql -u root -P 18001 進入MySQL命令行
.\bin\mysqld.exe --initialize-insecure --user=mysql
4、啟動服務
net start MySQL8_Master
5、配置主庫
-- 創建一個用于從庫復制的賬戶
CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;-- 鎖定表并記錄binlog位置 ,鎖定后主庫不可修改數據
FLUSH TABLES WITH READ LOCK;
查看主庫binlog信息,從庫需要使用
mysql> SHOW MASTER STATUS;+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 827 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
安裝從庫
我的安裝位置是 D:\module\mysql802
1、配置ini
文件
因為在一條電腦上,端口要與主庫不同,server-id 與主庫不同
[mysqld]
port=18002
basedir=D:\module\mysql802
datadir=D:\module\mysql802\data
server-id=2
relay-log=mysql-relay-bin
read_only=1
skip_slave_start=OFF
2、安裝服務
在根目錄(my.ini
所在的目錄)使用管理員權限打開cmd
或者powershell
.\bin\mysqld.exe --install MySQL8_Slave --defaults-file="D:\module\mysql802\my.ini"
3、初始化數據庫
.\bin\mysqld.exe --initialize-insecure --user=mysql
4、啟動服務
net start MySQL8_Slave
5、配置從庫
登錄 MySQL .\bin\mysql.exe -u root -P 18002
-- 連接主庫
mysql> CHANGE MASTER TO-> MASTER_HOST='127.0.0.1',-> MASTER_PORT=18001,-> MASTER_USER='repl',-> MASTER_PASSWORD='123456',-> MASTER_LOG_FILE='mysql-bin.000002',-> MASTER_LOG_POS=827;
Query OK, 0 rows affected, 8 warnings (0.07 sec)
-> MASTER_LOG_FILE=‘mysql-bin.000002’,
-> MASTER_LOG_POS=827;
這兩個參數是從主庫 SHOW MASTER STATUS 查詢到的
6、驗證從庫狀態
SHOW SLAVE STATUS\G -- 注意三個參數:
-- Slave_IO_Running: Yes
-- Slave_SQL_Running: Yes
-- Seconds_Behind_Master: 0
操作主庫驗證
主庫:
-- 先解鎖UNLOCK TABLES;-- 創建一個數據庫
mysql> create database demo01;
Query OK, 1 row affected (0.01 sec)mysql>
從庫:出現 demo01
數據庫,配置成功
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo01 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
主庫創建表,插入數據:
mysql> use demo01;
Database changed
mysql> show tables;
Empty set (0.01 sec)mysql> create table employee (-> id int primary key auto_increment,-> name varchar(100),-> age int,-> salary decimal(10,2)-> );
Query OK, 0 rows affected (0.02 sec)mysql> insert into employee (name,age,salary) values ('zhangsan',30,5000.00);
Query OK, 1 row affected (0.01 sec)mysql> select * from employee;
+----+----------+------+---------+
| id | name | age | salary |
+----+----------+------+---------+
| 1 | zhangsan | 30 | 5000.00 |
+----+----------+------+---------+
1 row in set (0.00 sec)
查詢從庫:
mysql> use demo01;
Database changed
mysql> select * from employee;
+----+----------+------+---------+
| id | name | age | salary |
+----+----------+------+---------+
| 1 | zhangsan | 30 | 5000.00 |
+----+----------+------+---------+
1 row in set (0.00 sec)mysql>