Docker安裝mysql5.7並且配置主從複製
阿新 • • 發佈:2018-12-22
轉載請表明出處 https://blog.csdn.net/Amor_Leo/article/details/85177001 謝謝
Docker安裝mysql5.7並且配置主從複製
拉取mysql映象
docker pull mysql:5.7
建立檔案docker.cnf
mysql主機(192.168.0.109)
建立資料夾
mkdir /usr/local/mysql
cd mysql/
mkdir conf data
建立docker.cnf
vim /usr/local/mysql/conf/docker.cnf
- 具體新增
[mysqld]
server-id=1
log-bin=master-bin #只是讀寫,就只要主庫配置即可.如果要做主從切換,那麼主庫和從庫都需要開啟.
skip-host-cache
skip-name-resolve
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
mysql從機(192.168.0.110)
建立資料夾
mkdir /usr/local/mysql
cd mysql/
mkdir conf data
建立docker.cnf
vim /usr/local/mysql/conf/docker.cnf
- 具體新增
[mysqld]
server-id=2
skip-host-cache
skip-name-resolve
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
建立容器並執行
mysql主機
docker run --name mysqlmaster -p 3306:3306 -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --privileged=true -d mysql:5.7
mysql從機
docker run --name mysqlslave -p 3306:3306 -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --privileged=true -d mysql:5.7
開啟主從複製
mysql主機
- 進入mysql容器
docker exec -it mysqlmaster /bin/bash
- 啟動mysql
mysql -uroot -proot
- 建立使用者
create user 'lhl'@'%' identified by 'root';
- 許可權
grant replication slave on *.* to 'lhl'@'%';
- 重新整理許可權
flush privileges;
- 檢視狀態
show master status;
mysql從機
-
進入mysql容器
docker exec -it mysqlslave /bin/bash
-
啟動mysql
mysql -uroot -proot
-
執行主從
change master to master_host='192.168.0.109',master_port=3306,master_user='lhl',master_password='root',master_log_file='master-bin.000003',master_log_pos=745;
- 如果報錯,執行下面命令並再次執行上面的命令
stop slave;
-
開啟複製
start slave;
-
檢視主從是否成功
show slave status \G;
以下兩個都為yes就代表成功了
Slave_IO_Running: Yes Slave_SQL_Running: Yes 說明主從成功