MySql8 Docker 读写分离

2021/11/23 19:13:26

本文主要是介绍MySql8 Docker 读写分离,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、主服务器配置

1.创建主服务器所需目录
mkdir -p /data/docker/mysql/master/cnf
mkdir -p /data/docker/mysql/master/data

2.定义主服务器配置文件
vim /data/docker/mysql/master/cnf/mysql.cnf

[mysqld]
## 设置server_id,注意要唯一
server-id=1
## 开启binlog
log-bin=mysql-bin
## binlog缓存
binlog_cache_size=1M
## binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed


3.创建并启动mysql主服务
docker run -itd -p 3306:3306 --name mysql_master -v /data/docker/mysql/master/cnf:/etc/mysql/conf.d -v /data/docker/mysql/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql

添加复制master数据的用户reader,供从服务器使用
mysql -u root -p123456

#mysql 老版本
#GRANT REPLICATION SLAVE ON *.* to 'reader'@'%' identified by 'reader###123';

#mysql 8 版本
mysql> CREATE USER 'reader'@'%' IDENTIFIED WITH mysql_native_password BY 'reader###123';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'reader'@'%';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'reader'@'%' ;
mysql> FLUSH PRIVILEGES;

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 156 | | | |
+---------------+----------+--------------+------------------+-------------------+

 

二、从服务器配置

4.创建从服务器所需目录,编辑配置文件
mkdir /data/docker/mysql/slave/cnf -p
mkdir /data/docker/mysql/slave/cnf -p
vim /data/docker/mysql/slave/cnf/mysql.cnf

[mysqld]
## 设置server_id,注意要唯一
server-id=2
## 开启binlog,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
## 如果需要同步函数或者存储过程
log_bin_trust_function_creators=true
## binlog缓存
binlog_cache_size=1M
## binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062

5.创建并运行mysql从服务器
docker run -itd -p 3306:3306 --name mysql_slaver -v /data/docker/mysql/slave/cnf:/etc/mysql/conf.d -v /data/docker/mysql/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql

7.从服务器上执行
mysql -u root -p123456

change master to master_host='192.168.137.200',master_user='reader',master_password='reader###123',master_log_file='binlog.000002',master_log_pos=156;

8.从服务器启动I/O 线程和SQL线程
start slave;

show slave status\G

 

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.137.200
Master_User: reader
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 1717
Relay_Log_File: edu-mysql-relay-bin.000002
Relay_Log_Pos: 1882
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1717
Relay_Log_Space: 2095
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: f76482c2-4c16-11ec-a420-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)



这篇关于MySql8 Docker 读写分离的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程