系統: Centos 7.4
數據庫版本:8.0.20html
安裝Dockermysql
export VERSION=18.06 && curl -fsSL http://rainbond-pkg.oss-cn-shanghai.aliyuncs.com/releases/docker/install-docker.sh | bash -s docker
啓動並開機自啓redis
systemctl start docker systemctl enable docker
拉取鏡像sql
docker pull mysql
附配置文件,在容器啓動時分別掛載主從的配置文件docker
主數據庫
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # 服務端默認utf8編碼 character-set-server=utf8mb4 # 默認存儲引擎 default-storage-engine=INNODB # 主從配置 log-bin=binlog server-id=1 gtid-mode=on enforce-gtid-consistency=on log-slave-updates=on [client] #設置客戶端編碼 default-character-set=utf8mb4 [mysql] # 設置mysql客戶端默認編碼 default-character-set=utf8mb4 # Custom config should go here !includedir /etc/mysql/conf.d/ # Custom config should go here !includedir /etc/mysql/conf.d/
從bash
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # 服務端默認utf8編碼 character-set-server=utf8mb4 # 默認存儲引擎 default-storage-engine=INNODB # 主從配置 server-id=2 gtid-mode=on enforce-gtid-consistency=on log-slave-updates=on [client] #設置客戶端編碼 default-character-set=utf8mb4 [mysql] # 設置mysql客戶端默認編碼 default-character-set=utf8mb4 # Custom config should go here !includedir /etc/mysql/conf.d/ # Custom config should go here !includedir /etc/mysql/conf.d/
啓動數據庫服務器
docker run --name mysql_master --restart=always -p 3306:3306 --privileged=true -v /root/mysql/my.cnf:/etc/mysql/my.cnf -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
查看數據庫字符編碼(可選),並建立用戶受權curl
# 進入數據庫 docker exec -it mysql_master bash # 查看字符編碼 mysql> show global variables like'%character_set%'; # 建立用戶受權 mysql> CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'slave'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%'; mysql> flush privileges;
獲取主節點當前binary log文件名和位置(position)socket
mysql> SHOW MASTER STATUS; +---------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+------------------------------------------+ | binlog.000003 | 868 | | | 1b009ef8-a67f-11ea-8c9a-0242ac110002:1-8 | +---------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
啓動數據庫
docker run --name mysql_slave --restart=always -p 3306:3306 --privileged=true -v /root/mysql/my.cnf:/etc/mysql/my.cnf -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
配置主從複製
# 進入數據庫 docker exec -it mysql_slave bash # 主從配置 mysql> CHANGE MASTER TO mysql> MASTER_HOST='192.168.0.162', mysql> MASTER_USER='slave', mysql> MASTER_PASSWORD='slave', mysql> MASTER_PORT=3306, mysql> MASTER_LOG_FILE='binlog.000003', mysql> MASTER_LOG_POS=868; # 開啓主從同步 mysql> start slave; # 再查看主從同步狀態 mysql> show slave status;
這裏只要看到兩個參數Slave_IO_Running和Slave_SQL_Running都爲true且Error字段都爲空則代碼主從正常複製
經過在主服務器建立數據庫建表插入數據的方式來進行測試,查看從服務器是否同步更新了數據
在主庫建立庫
mysql> create database kong;
在從庫查看
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | kong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
數據同步成功,主從複製部署完成