11.MySQL主从配置
# 01.安装MySQL主从服务器
# 1.1 初始化docker中MySQL挂载目录
# 新建2个目录, 存放master和slave的配置和数据
cd ~ # ~ 代表家目录 /home/你的用户名/
mkdir MySQL_master
mkdir MySQL_slave
cd MySQL_master
mkdir data
cp -r /etc/MySQL/MySQL.conf.d ./
cd ../MySQL_slave
mkdir data
cp -r /etc/MySQL/MySQL.conf.d ./
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 1.2 MySQL主机配置和从机配置
- 主机配置
# vim /root/MySQL_master/MySQL.conf.d/MySQLd.cnf
# 端口
port = 3307
# 开启日志
general_log_file = /var/log/MySQL/MySQL.log
general_log = 1
# 主机唯一编号
server-id = 1
# binlog日志文件
log_bin = /var/log/MySQL/MySQL-bin.log
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 从机配置
# 从机配置 /root/MySQL_slave/MySQL.conf.d/MySQLd.cnf
port = 3308
general_log = 0
server-id = 2
1
2
3
4
2
3
4
# 1.3 使用docker安装MySQL主从
#1.下载MySQL 5.7.32版本的MySQL
docker pull MySQL:5.7.32 # 直接通过官方下载
#2.启动master
sudo docker run -d --name MySQL-master -e MySQL_ROOT_PASSWORD=1 --network=host -v /root/MySQL_master/data:/var/lib/MySQL -v /root/MySQL_master/MySQL.conf.d:/etc/MySQL/MySQL.conf.d MySQL:5.7.32
#3.启动slave
sudo docker run -d --name MySQL-slave -e MySQL_ROOT_PASSWORD=1 --network=host -v /root/MySQL_slave/data:/var/lib/MySQL -v /root/MySQL_slave/MySQL.conf.d:/etc/MySQL/MySQL.conf.d MySQL:5.7.32
#4.测试master/slave (密码是1)
MySQL -uroot -p1 -h 192.168.56.100 --port=3307
MySQL -uroot -p1 -h 192.168.56.100 --port=3308
#5.导出导入数据
#5.1 从主机导出
MySQLdump -uroot -p1 -h192.168.56.100 -P3307 --all-databases --lock-all-tables > ~/master_db.sql
#5.2 导入从机
MySQL -uroot -p1 -h192.168.56.100 -P3308 < ~/master_db.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1.4 配置MySQL主从
配置MySQL master
# 配置master
# 登录到主机
MySQL –uroot –p -h 192.168.56.100 -P 3307
# 创建从机账号
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' identified by 'slave';
# 刷新权限
FLUSH PRIVILEGES;
# 查看二进制日志信息, 记录 文件名 和 偏移量, 后面会用到
MySQL> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| MySQL-bin.000003 | 722 | | | |
+------------------+----------+--------------+------------------+-------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
配置MySQL slave
# 配置slave
# 登录到从机
$ MySQL -uroot -p1 -h192.168.56.100 -P3308
# 从机连接到主机
$ change master to master_host='192.168.56.100', master_port=3307, master_user='slave', master_password='slave',master_log_file='MySQL-bin.000003', master_log_pos=722;
# 开启从机服务
$ start slave;
# 展示从机服务状态
$ show slave status \G
MySQL> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.100
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 722
Relay_Log_File: dev-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: MySQL-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 02.django使用MySQL主从
# 2.1 django中配置MySQL主从
# 配置django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.MySQL',
'NAME': 'syl',
'HOST': '127.0.0.1',
'PORT': '3307',
'USER': 'root',
'PASSWORD': '1',
},
'slave': {
'ENGINE': 'django.db.backends.MySQL',
'NAME': 'syl',
'HOST': '127.0.0.1',
'PORT': '3308',
'USER': 'root',
'PASSWORD': '1',
}
}
# 数据库路由配置
DATABASE_ROUTERS = ['utils.db_router.MasterSlaveDBRouter']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2.2 编写MySQL路由文件
# utils/db_router.py
class MasterSlaveDBRouter(object):
"""数据库读写路由"""
def db_for_read(self, model, **hints):
"""读"""
return "slave"
def db_for_write(self, model, **hints):
"""写"""
return "default"
def allow_relation(self, obj1, obj2, **hints):
"""是否运行关联操作"""
return True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上次更新: 2024/10/15 16:27:13