接上一篇:Mysql安装(Docker)
目录
环境准备
1. 按上一篇安装mysql
2. 创建目录
3. 准备配置文件
启动Mysql服务
1. 启动mysql master slave1 slave2容器
2. 检查各mysql是否启动(以master为例)
mysql 主从配置
1. 修改主从配置文件(主开启log-bin日志,从开启只读)
2. 重启master容器,并查看log_bin是否开启、添加backup用户
3. 重启slave,并添加master相关配置
4. 登录master 查看master、slave状态
5. 验证主从同步
#创建以下目录(文件目录与my.cnf中配置项对应)
/usr/local/mysql/master #mysql master
|--conf
|--data
|--log
/usr/local/mysql/slave1 #mysql slave
|--conf
|--data
|--log
/usr/local/mysql/slave2
|--conf
|--data
|--log
cd /usr/local/mysql/
# 使docker容器有写入权限
chown -R polkitd:input *
#主
[root@localhost mysql]# more master/conf/my.cnf
[client]
socket=/apps/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=/apps/mysql
datadir=/apps/mysql/data
log-error=/apps/mysql/log/error.log
socket=/apps/mysql/mysql.sock
pid-file=/apps/mysql/mysql.pid
character_set_server=utf8mb4
interactive_timeout = 120
wait_timeout = 120
max_allowed_packet = 32M
server_id=1
========================================================
#从
[root@localhost mysql]# more slave1/conf/my.cnf
[client]
socket=/apps/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=/apps/mysql
datadir=/apps/mysql/data
log-error=/apps/mysql/log/error.log
socket=/apps/mysql/mysql.sock
pid-file=/apps/mysql/mysql.pid
character_set_server=utf8mb4
interactive_timeout = 120
wait_timeout = 120
max_allowed_packet = 32M
server_id=2
========================================================
[root@localhost mysql]# more slave2/conf/my.cnf
[client]
socket=/apps/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=/apps/mysql
datadir=/apps/mysql/data
log-error=/apps/mysql/log/error.log
socket=/apps/mysql/mysql.sock
pid-file=/apps/mysql/mysql.pid
character_set_server=utf8mb4
interactive_timeout = 120
wait_timeout = 120
max_allowed_packet = 32M
server_id=3
# -e TZ指定时区 -p 映射端口 -v 挂载本地目录 -d 后台运行
#master
[root@localhost master]# docker run --name mysql_master -e TZ="Asia/Shanghai" -p 3301:3306 -v /usr/local/mysql/master/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/master/:/apps/mysql --privileged=true -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
#slave1
[root@localhost master]# docker run --name mysql_slave1 -e TZ="Asia/Shanghai" -p 3302:3306 -v /usr/local/mysql/slave1/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/slave1/:/apps/mysql --privileged=true -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
#slave2
[root@localhost master]# docker run --name mysql_slave2 -e TZ="Asia/Shanghai" -p 3303:3306 -v /usr/local/mysql/slave2/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/slave2/:/apps/mysql --privileged=true -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd9a352fd0dc mysql:5.7.21 "docker-entrypoint.s…" About an hour ago Up 9 minutes 0.0.0.0:3303->3306/tcp mysql_slave2
875f704ee47f mysql:5.7.21 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3302->3306/tcp mysql_slave1
355441667c89 mysql:5.7.21 "docker-entrypoint.s…" 23 hours ago Up 4 hours 0.0.0.0:3301->3306/tcp mysql_master
[root@localhost ~]#
[root@localhost ~]# docker exec -it mysql_master /bin/bash
root@355441667c89:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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>
#master
[root@localhost mysql]# more master/conf/my.cnf
[client]
socket=/apps/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=/apps/mysql
datadir=/apps/mysql/data
log-error=/apps/mysql/log/error.log
socket=/apps/mysql/mysql.sock
pid-file=/apps/mysql/mysql.pid
character_set_server=utf8mb4
interactive_timeout = 120
wait_timeout = 120
max_allowed_packet = 32M
server-id=1
#开启log-bin日志
log-bin=mysql-bin
#binlog-do-db=
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
========================================================
#从
[root@localhost mysql]# more slave1/conf/my.cnf
[client]
socket=/apps/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=/apps/mysql
datadir=/apps/mysql/data
log-error=/apps/mysql/log/error.log
socket=/apps/mysql/mysql.sock
pid-file=/apps/mysql/mysql.pid
character_set_server=utf8mb4
interactive_timeout = 120
wait_timeout = 120
max_allowed_packet = 32M
server_id=2
master_info_repository=TABLE
relay_log_info_repository=TABLE
read_only=1
super_read_only=1
relay_log_recovery=1
========================================================
[root@localhost mysql]# more slave2/conf/my.cnf
[client]
socket=/apps/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=/apps/mysql
datadir=/apps/mysql/data
log-error=/apps/mysql/log/error.log
socket=/apps/mysql/mysql.sock
pid-file=/apps/mysql/mysql.pid
character_set_server=utf8mb4
interactive_timeout = 120
wait_timeout = 120
max_allowed_packet = 32M
server_id=3
master_info_repository=TABLE
relay_log_info_repository=TABLE
read_only=1
super_read_only=1
relay_log_recovery=1
[root@localhost ~]# docker restart mysql_master
[root@localhost ~]# docker exec -it mysql_master /bin/bash
[root@355441667c89:/# mysql -uroot -p
Enter password:
#添加backup用户,密码123456
#查看log bin是否开启
mysql> SHOW VARIABLES LIKE '%log_bin%';
+---------------------------------+----------------------------------+
| Variable_name | Value |
+---------------------------------+----------------------------------+
| log_bin | ON |
| log_bin_basename | /apps/mysql/data/mysql-bin |
| log_bin_index | /apps/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+----------------------------------+
6 rows in set (0.01 sec)
#查看master状态,主要是File和Position,要与从配置相同
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000006 | 154 | | mysql,information_schema,performance_schema,sys | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)
#添加backup用户,用于从同步数据
mysql> grant replication slave on *.* to 'backup'@'%' identified by '123456';
#查询用户及访问权限
mysql> use mysql
mysql> select user,authentication_string,host from user;
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# slave1与slave2 操作相同
[root@localhost conf]# docker restart mysql_slave1
[root@localhost conf]# docker exec -it mysql_slave1 /bin/bash
[root@cd9a352fd0dc:/# mysql -uroot -p
Enter password:
mysql> show slave status;
mysql> stop slave;
-----------------------------------------------
change master to
master_host='172.17.0.2',
master_user='backup',
master_password='123456',
master_log_file='mysql-bin.000006',
master_log_pos=458;
-------------------------------------------------
mysql> change master to
-> master_host='172.17.0.2',
-> master_user='backup',
-> master_password='123456',
-> master_log_file='mysql-bin.000006',
-> master_log_pos=458;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
# 重点查看Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 458
Relay_Log_File: cd9a352fd0dc-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000006
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: 458
Relay_Log_Space: 534
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: 7d108ff6-86a5-11e9-915f-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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:
1 row in set (0.00 sec)
mysql>
ps:docker容器ip查看
docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql_master
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000006 | 458 | | mysql,information_schema,performance_schema,sys | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 3 | | 3306 | 1 | 0f9bc301-875b-11e9-9348-0242ac110004 |
| 2 | | 3306 | 1 | 9682bfa8-875a-11e9-ac11-0242ac110003 |
+-----------+------+------+-----------+--------------------------------------+
2 rows in set (0.00 sec)
在master 建库 建表 插入数据,查看slave是否同步(略)