方案,docker容器化部署主從mysql
centos7.6,docker-1.13.1,mariadb:latestlinux
#yum install yum-utils device-mapper-persistent-data lvm2 -y
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#yum install docker-ce -y
# 修改daemon配置文件/etc/docker/daemon.json來使用加速器
#sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://vb10fi3t.mirror.aliyuncs.com"]
}
EOF
#systemctl daemon-reload
#systemctl restart docker
#拉取mariadb鏡像
#docker pull mariadb
複製代碼
#mkdir -pv /data/mysql/{master/{data,conf},slave/{data,conf}}
#touch /mysql/master/conf/my.cnf
#touch /mysql/slave/conf/my.cnf
#tree mysql
複製代碼
......
[mysqld]
server_id = 1
log-bin= mysql-bin
read-only=0
......
複製代碼
......
[mysqld]
server_id = 2
log-bin= mysql-bin
read-only=0
......
複製代碼
#docker run --name master -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123 -v /data/mysql/master/data:/var/lib/mysql -v /data/mysql/master/conf/my.cnf:/etc/my.cnf --privileged=true -d mariadb
複製代碼
#docker run --name slave -p 3309:3306 -e MYSQL_ROOT_PASSWORD=123 -v /data/mysql/slave/data:/var/lib/mysql -v /data/mysql/slave/conf/my.cnf:/etc/my.cnf --privileged=true -d mariadb
複製代碼
//進入容器master
#docker exec -it master bash
//登入mysql
#mysql -uroot -p
password:123
//建立一個用戶來同步數據,每一個slave使用標準的MySQL用戶名和密碼鏈接master。進行復制操做的用戶會授予REPLICATION SLAVE 權限。
> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
//建立一個slave同步帳號slave,容許訪問的IP地址爲%
> grant replication slave on *.* to 'slave'@'%' identified by '123456';
//查看狀態,記住File、Position的值,在Slave中將用到
show master status\G
複製代碼
//進入容器slave
#docker exec -it slave bash
//登入mysql
#mysql -uroot -p
password:123
//設置主庫連接
> change master to master_host='172.17.0.2',master_user='slave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3306;
//啓動從庫同步
> start slave;
//查看slave狀態
show slave status\G
......
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
上述兩項都爲YES 表示主從同步OK
複製代碼
若是出現錯誤,請滑動到最下方問題👇↓sql
centos7.6,docker-1.13.1,mariadb:latestdocker
#docker pull mariadb
複製代碼
#mkdir -pv /data/mysql/{master_1/{data,conf},master_2/{data,conf}}
#touch /mysql/master_1/conf/my.cnf
#touch /mysql/master_2/conf/my.cnf
#tree mysql
複製代碼
......
[mysqld]
server_id = 1
log-bin= mysql-bin
read-only=0
auto_increment_offset=1
auto_increment_increment=2
......
複製代碼
......
[mysqld]
server_id = 2
log-bin= mysql-bin
read-only=0
auto_increment_offset=2
auto_increment_increment=2
......
複製代碼
#docker run --name master -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123 -v /data/mysql/master_1/data:/var/lib/mysql -v /data/mysql/master_1/conf/my.cnf:/etc/my.cnf --privileged=true -d mariadb
複製代碼
#docker run --name slave -p 3309:3306 -e MYSQL_ROOT_PASSWORD=123 -v /data/mysql/master_2/data:/var/lib/mysql -v /data/mysql/master_2/conf/my.cnf:/etc/my.cnf --privileged=true -d mariadb
複製代碼
//進入容器master_1
#docker exec -it master_1
#mysql -u root -p
password:123
//建立一個用戶來同步數據.這裏表示建立一個slave同步帳號slave,容許訪問的IP地址爲%,%表示通配符
GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456';
//查看狀態,記住File、Position的值
show master status;
複製代碼
//進入容器master_2
docker exec -it master_2
#mysql -u root -p
password:123
//設置主庫連接,master_host即爲容器IP,master_log_file和master_log_pos即爲在maseter_2容器中,經過show master status查出來的值;
change master to master_host='172.17.0.2',master_user='slave',master_password='123456',master_log_file='mysql-bin.000005',master_log_pos=536,master_port=3306;
//建立一個用戶來同步數據
GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456';
//啓動同步
start slave ;
//查看狀態
show master status;
複製代碼
//進入master_1容器
docker exec -it master_1
#mysql -u root -p
password:123
//設置master_2主庫連接,參數詳細說明同上
> change master to master_host='172.17.0.3',master_user='slave',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=536,master_port=3306;
//啓動同步
> start slave ;
複製代碼
> create database demo_03;
> use demo_03;
> create table user_info(name varchar(20),age int);
> insert into user_info values('abc',1000);
> select name,age from userinfo;
複製代碼
> select name,age from demo_03.user_info;
複製代碼
若是兩次數據一致,表示主主同步成功。json
問題一 執行docker run image 使用docker ps 查看,結果並無啓動中的容器centos
#docker logs master
find: '/var/lib/mysql/': Permission denied
chown: changing ownership of '/var/lib/mysql/': Permission denied
複製代碼
--privileged=true #給容器加上特定權限;解決問題bash
關閉selinux # 我嘗試了,沒解決問題架構
問題二 Slave_SQL_Running: No
app
方案一 :ide
stop slave ;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave ;
方案二:
查看master狀態
show master status;
file項:mysql-bin.000003
進入slave
stop slave;
change master to master_host='master_ip',master_user='user',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=0,master_port=3306;
start slave;