您好,登錄后才能下訂單哦!
Docker 中怎么實現MySQL主主復制,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
Install Docker Engine on CentOS
Master
新建目錄data(XXXX/master/data),映射docker數據庫目錄
新建配置(XXXX/master/my.cnf)
[mysqld] skip-host-cache skip-name-resolve datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock secure-file-priv=/var/lib/mysql-files user=mysql pid-file=/var/run/mysqld/mysqld.pid # replication # server id 唯一 server_id=1 # STATEMENT, ROW, or MIXED binlog_format=ROW log_bin=/var/lib/mysql/master-bin auto-increment-increment=2 auto-increment-offset=1 slave-skip-errors=all # 不進行復制的數據庫 replicate-ignore-db=information_schema replicate-ignore-db=mysql replicate-ignore-db=performance_schema replicate-ignore-db=sys
Replica
新建目錄data(XXXX/rep/data),映射docker數據庫目錄
新建配置(XXXX/rep/my.cnf)
[mysqld] skip-host-cache skip-name-resolve datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock secure-file-priv=/var/lib/mysql-files user=mysql pid-file=/var/run/mysqld/mysqld.pid # replication # server id 唯一 server_id=2 # STATEMENT, ROW, or MIXED binlog_format=ROW log_bin=/var/lib/mysql/rep-bin auto-increment-increment=2 auto-increment-offset=1 slave-skip-errors=all # 不進行復制的數據庫 replicate-ignore-db=information_schema replicate-ignore-db=mysql replicate-ignore-db=performance_schema replicate-ignore-db=sys
docker network create mysql_net
mysql服務連接到新增的網絡mysql_net
mysql服務可以通過主機名互相訪問
Master
docker container run --name mysql_master \ --network mysql_net \ --hostname mysql_master \ --publish 33306:3306 \ --env MYSQL_ROOT_PASSWORD=123456 \ --mount type=bind,src=/xxxx/master/my.cnf,dst=/etc/my.cnf \ --mount type=bind,src=/xxxx/master/data,dst=/var/lib/mysql \ -d mysql/mysql-server:8.0 --character-set-server=utf8 --collation-server=utf8_general_ci --explicit_defaults_for_timestamp
Replica
docker container run --name mysql_rep \ --network mysql_net \ --hostname mysql_rep \ --publish 43306:3306 \ --env MYSQL_ROOT_PASSWORD=123456 \ --mount type=bind,src=/xxx/repl/my.cnf,dst=/etc/my.cnf \ --mount type=bind,src=/xxx/repl/data,dst=/var/lib/mysql \ -d mysql/mysql-server:8.0 --character-set-server=utf8 --collation-server=utf8_general_ci --explicit_defaults_for_timestamp
參數說明
XXX
:替換為有效的路徑
network
:指定為新增的網絡mysql_net
hostname
:服務主機名
publish
:端口映射([docker host Port]:[container Port])
MYSQL_ROOT_PASSWORD
: MySQL默認密碼
mount
:掛載配置文件及數據庫文件目錄
查看MySQL服務是否正常啟動
# 查看Docker container進程 docker container ps -a | grep mysql # 查看啟動日志 docker container logs mysql_master docker container logs mysql_rep
新增具復制權限的用戶
登錄主數據庫
# 登入 mysql_master docker container exec -it mysql_master bash # 連接數據庫,密碼為配置文件中指定的123456 mysql -u root -p # 新增復制用戶并賦復制權限 CREATE USER 'dev'@'%' IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'dev'@'%';
登錄備數據庫
# 登入 mysql_rep docker container exec -it mysql_rep bash # 連接數據庫,密碼為配置文件中指定的123456 mysql -u root -p # 新增復制用戶并賦復制權限 CREATE USER 'dev'@'%' IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'dev'@'%';
分別查看主從服務數據庫狀態
# 主數據庫 mysql> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000005 | 445 | | | | +-------------------+----------+--------------+------------------+-------------------+ # 備數據庫 mysql> SHOW MASTER STATUS; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | rep-bin.000002 | 155 | | | | +----------------+----------+--------------+------------------+-------------------+
配置數據庫復制
配置備數據庫mysql_rep
# MASTER_HOST:主數據庫服務名 # MASTER_LOG_FILE: 日志文件名稱 # MASTER_LOG_POS: 日志文件當前位置 mysql> CHANGE MASTER TO MASTER_HOST='mysql_master', MASTER_USER='dev', MASTER_PASSWORD='123456', MASTER_LOG_FILE='master-bin.000003', MASTER_LOG_POS=692;
配置主數據庫mysql_master
# MASTER_HOST: 從數據庫服務名 # MASTER_LOG_FILE: 日志文件名稱 # MASTER_LOG_POS: 日志文件當前位置 mysql> CHANGE MASTER TO MASTER_HOST='mysql_rep', MASTER_USER='dev', MASTER_PASSWORD='123456', MASTER_LOG_FILE='rep-bin.000001', MASTER_LOG_POS=155;
啟動復制
備數據庫
mysql> start slave;
主數據庫
mysql> start slave;
查看數據庫狀態
mysql> show master status; mysql> show slave status;
看完上述內容,你們掌握Docker 中怎么實現MySQL主主復制的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。