一 准备好虚拟机,安装docker
1 关闭防火墙和SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
2 添加docker的repo源,并下载docker
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-bash: yum-config-manager: 未找到命令 //一直显示找不到命令可以用wget解决
[root@localhost ~]# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# mv /root/docker-ce.repo ./
[root@localhost yum.repos.d]# yum install docker-ce -y
二 安装mysql镜像
1 启动docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl status docker
2 拉去mysql镜像(mysql 8.0版本)
镜像连接 https://docker.aityp.com/image/docker.io/mysql:8.0(里面含有多个镜像)
[root@localhost ~]# docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql:8.0
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql 8.0 1eba4c9bcaa8 3 months ago 567MB
[root@localhost ~]# docker tag swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql:8.0 docker.io/mysql:8.0
[root@localhost ~]# docker rmi swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql:8.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0 1eba4c9bcaa8 3 months ago 567MB
三 启动MySQL(master节点的配置)
,1 设置配置文件
[mysqld]
log-bin=mysql-bin
#标识唯一id(必须),一般使用ip最后位
server-id=1
#不同步的数据库,可设置多个
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#指定需要同步的数据库(和slave是相互匹配的),可以设置多个
#binlog-do-db=test #我们这里需要同步所有库,所以这里不设置
2 启动数据库
[root@localhost master]# docker run -d -p 3306:3306 --privileged=true \
> -v /mysql/master/log:/var/log/mysql \
> -v /mysql/master/data:/var/lib/mysql \
> -v /mysql/master/conf:/etc/mysql/conf.d \
> -e MYSQL_ROOT_PASSWORD=123456 \
> --name mysql-master \
> mysql:8.0;
12431d09c55289fda97c546673fa314a6527760271871020ea50900579bd24d4
查看正在运行的容器
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12431d09c552 mysql:8.0 "docker-entrypoint.s…" 34 seconds ago Up 33 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-master
进入MySQL容器,配置相关信息
[root@localhost conf]# docker exec -it mysql-master bash
bash-5.1# mysql -u root -p
创建用户名,添加相应的权限
CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; //创建应该能够远程访问的用户名
GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%'; //授予该用户名对所有库和表修改的权限
FLUSH PRIVILEGES; //刷新权限
查看mater的状态
show status master
+------------------+----------+--------------+---------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+---------------------------------------------+-------------------+
| mysql-bin.000001 | 1072 | | information_schema,performance_schema,mysql | |
+------------------+----------+--------------+---------------------------------------------+-------------------+
1 row in set (0.00 sec)
四 配置从数据库(slave)
1 创建挂载目录
[root@localhost ~]# mkdir /mysql/slave/conf -p
[root@localhost ~]# mkdir /mysql/slave/log -p
[root@localhost ~]# mkdir /mysql/slave/data -p
[root@localhost ~]# cd /mysql/slave/conf/
[root@localhost conf]# vim my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#与主库配置保持一致
#replicate-do-db=test #这里省略,因为主库是同步所有
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2 重新开其一个数据库
docker run -d -p 3307:3306 --privileged=true \
-v /mysql/slave/log:/var/log/mysql \
-v /mysql/slave/data:/var/lib/mysql \
-v /mysql/slave/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql-slave \
mysql:8.0
9a4f10cb1f130bc14fcb55543a963847ae40611a27c139c184d279802049fdef
3 查看启动的数据库
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a4f10cb1f13 mysql:8.0 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-slave
12431d09c552 mysql:8.0 "docker-entrypoint.s…" 44 minutes ago Up 36 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-master
4 进入数据库
[root@localhost conf]# docker exec -it mysql-slave bash
bash-5.1# mysql -u root -p
5 配置slave的相关信息
change master to master_host='192.168.243.24',master_user='root',master_password='123456',master_log_file='mysql-bin.000002', master_log_pos=157; //默认端口是3306,可以不写
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.243.24
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1257
Relay_Log_File: 9a4f10cb1f13-relay-bin.000001
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
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: 1257
Relay_Log_Space: 543
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: 93a3683b-56d5-11ef-91c1-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)
五 测试
1 master节点创建数据库db01
//master节点
mysql> create database db01;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db01 |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql>
slave节点查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db01 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql>
同步成功