在docker中安裝mysql並搭建主從複製
在docker中搭建一主兩從的mysql複製環境,步驟如下:
1.docker mysql的安裝
[root@localhost docker]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 4306 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 294 [OK]
centurylink/mysql Image containing mysql. Optimized to be li... 51 [OK]
zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 39 [OK]
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 21 [OK]
imega/mysql Size: 149 MB, alpine:3.5, Mysql Server: 10... 11 [OK]
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
marvambass/mysql MySQL Server based on Ubuntu 14.04 7 [OK]
zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 7 [OK]
bitnami/mysql Bitnami MySQL Docker Image 5 [OK]
dnhsoft/mysql-utf8 Inherits the official MySQL image configur... 5 [OK]
debezium/example-mysql Example MySQL database server with a simpl... 4 [OK]
frodenas/mysql A Docker Image for MySQL 3 [OK]
alterway/mysql Docker Mysql 3 [OK]
yfix/mysql Yfix docker built mysql 2 [OK]
drupaldocker/mysql MySQL for Drupal 2 [OK]
coscale/mysql CoScale custom configuration of the offici... 1 [OK]
lysender/mysql MySQL base image using Ubuntu 16.04 Xenial 1 [OK]
tozd/mysql MySQL (MariaDB fork) Docker image. 1 [OK]
projectomakase/mysql Docker image for MySQL 0 [OK]
1maa/mysql MySQL database 0 [OK]
datajoint/mysql MySQL image pre-configured to work smoothl... 0 [OK]
captomd/mysql CaptoMD mysql configuration 0 [OK]
cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]
nanobox/mysql MySQL service for nanobox.io 0 [OK]
nanobox/mysql MySQL service for nanobox.io 0 [OK]
[root@localhost docker]# docker pull mysql:5.7
5.7: Pulling from library/mysql
cd0a524342ef: Downloading 21.63 MB/52.55 MB
d9c95f06c17e: Download complete
46b2d578f59a: Download complete
10fbc2bcc6e9: Download complete
91b1a29c3956: Download complete
5bf9316bd602: Download complete
69bd23f08b55: Download complete
4fb778132e94: Downloading 18.38 MB/78.96 MB
6913628d7744: Download complete
a477f36dc2e0: Download complete
c954124ae935: Download complete
2.建立並執行三個mysql容器
docker run -p 3306:3306 --name master -v /app/mysql/conf/my_master.cnf:/etc/mysql/my.cnf -v /app/mysql/master:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker run -p 3307:3306 --name slave1 -v /app/mysql/conf/my_slave1.cnf:/etc/mysql/my.cnf -v /app/mysql/slave1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker run -p 3308:3306 --name slave2 -v /app/mysql/conf/my_slave2.cnf:/etc/mysql/my.cnf -v /app/mysql/slave2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
3.在host上安裝mysql客戶端
[root@localhost ~]# yum --enablerepo=mysql57-community clean metadata
[root@localhost ~]# yum install -y mysql-community-client
4.連線到docker上的mysql
[root@localhost mysql]# mysql -uroot -p -h127.0.0.1 -P3306
5.連線到docker的mysql容器
docker exec -it master bash
分別檢視三個容器的ip
172.17.0.2,172.17.0.3,172.17.0.4
6.配置主從複製關係
#在主庫172.17.0.2上建立使用者
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected (0.11 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
Query OK, 0 rows affected (0.05 sec)
#檢視主庫日誌位置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.08 sec)
#分別在兩個從庫上啟動複製
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.17.0.2',
-> MASTER_PORT=3306,
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl',
-> MASTER_LOG_FILE='mysql-bin.000003',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 1 warning (0.57 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
1.docker mysql的安裝
[root@localhost docker]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 4306 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 294 [OK]
centurylink/mysql Image containing mysql. Optimized to be li... 51 [OK]
zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 39 [OK]
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 21 [OK]
imega/mysql Size: 149 MB, alpine:3.5, Mysql Server: 10... 11 [OK]
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
marvambass/mysql MySQL Server based on Ubuntu 14.04 7 [OK]
zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 7 [OK]
bitnami/mysql Bitnami MySQL Docker Image 5 [OK]
dnhsoft/mysql-utf8 Inherits the official MySQL image configur... 5 [OK]
debezium/example-mysql Example MySQL database server with a simpl... 4 [OK]
frodenas/mysql A Docker Image for MySQL 3 [OK]
alterway/mysql Docker Mysql 3 [OK]
yfix/mysql Yfix docker built mysql 2 [OK]
drupaldocker/mysql MySQL for Drupal 2 [OK]
coscale/mysql CoScale custom configuration of the offici... 1 [OK]
lysender/mysql MySQL base image using Ubuntu 16.04 Xenial 1 [OK]
tozd/mysql MySQL (MariaDB fork) Docker image. 1 [OK]
projectomakase/mysql Docker image for MySQL 0 [OK]
1maa/mysql MySQL database 0 [OK]
datajoint/mysql MySQL image pre-configured to work smoothl... 0 [OK]
captomd/mysql CaptoMD mysql configuration 0 [OK]
cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]
nanobox/mysql MySQL service for nanobox.io 0 [OK]
nanobox/mysql MySQL service for nanobox.io 0 [OK]
[root@localhost docker]# docker pull mysql:5.7
5.7: Pulling from library/mysql
cd0a524342ef: Downloading 21.63 MB/52.55 MB
d9c95f06c17e: Download complete
46b2d578f59a: Download complete
10fbc2bcc6e9: Download complete
91b1a29c3956: Download complete
5bf9316bd602: Download complete
69bd23f08b55: Download complete
4fb778132e94: Downloading 18.38 MB/78.96 MB
6913628d7744: Download complete
a477f36dc2e0: Download complete
c954124ae935: Download complete
2.建立並執行三個mysql容器
docker run -p 3306:3306 --name master -v /app/mysql/conf/my_master.cnf:/etc/mysql/my.cnf -v /app/mysql/master:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker run -p 3307:3306 --name slave1 -v /app/mysql/conf/my_slave1.cnf:/etc/mysql/my.cnf -v /app/mysql/slave1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker run -p 3308:3306 --name slave2 -v /app/mysql/conf/my_slave2.cnf:/etc/mysql/my.cnf -v /app/mysql/slave2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
3.在host上安裝mysql客戶端
[root@localhost ~]# yum --enablerepo=mysql57-community clean metadata
[root@localhost ~]# yum install -y mysql-community-client
4.連線到docker上的mysql
[root@localhost mysql]# mysql -uroot -p -h127.0.0.1 -P3306
5.連線到docker的mysql容器
docker exec -it master bash
分別檢視三個容器的ip
172.17.0.2,172.17.0.3,172.17.0.4
6.配置主從複製關係
#在主庫172.17.0.2上建立使用者
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected (0.11 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
Query OK, 0 rows affected (0.05 sec)
#檢視主庫日誌位置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.08 sec)
#分別在兩個從庫上啟動複製
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.17.0.2',
-> MASTER_PORT=3306,
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl',
-> MASTER_LOG_FILE='mysql-bin.000003',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 1 warning (0.57 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10972173/viewspace-2138962/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL(14)---Docker搭建MySQL主從複製(一主一從)MySqlDocker
- docker-compase搭建mysql主從複製DockerMySql
- mysql主從複製搭建MySql
- MySQL單機多例項安裝並配置主從複製MySql
- Docker Compose搭建MySQL主從複製叢集DockerMySql
- Mysql 8.4.0 結合 Docker 搭建GTID主從複製,以及傳統主從複製MySqlDocker
- MySQL 主從複製安裝部署配置MySql
- MYSQL主從複製的搭建MySql
- 基於 Docker 的 MySQL 主從複製搭建(真正弄懂)DockerMySql
- MySQL 5.7的安裝及主從複製(主從同步)MySql主從同步
- docker實現mysql主從複製DockerMySql
- Docker 中 MySQL8 主從複製手記DockerMySql
- Docker安裝mysql映象並進行主從配置DockerMySql
- Mysql主從複製原理及搭建MySql
- 簡單搭建MySQL主從複製MySql
- Docker 教程十五配置MySQL 主從複製DockerMySql
- MySQL5.7主從複製-半同步複製搭建MySql
- 基於 Docker 的 MySQL 主從複製搭建及原理(真正弄懂)DockerMySql
- mysql主從複製的理解和搭建MySql
- mysql 5.7 主從複製搭建及原理MySql
- MySQL主從複製與主主複製MySql
- 使用 Docker Compose 搭建 MySQL 資料庫主從複製例項DockerMySql資料庫
- MySQL的主從複製與MySQL的主主複製MySql
- mysql5.7主從複製,主主複製MySql
- MySQL主從複製、半同步複製和主主複製MySql
- MYSQL主主複製的搭建MySql
- mysql複製--主從複製配置MySql
- MySQL 主從複製MySql
- 【MySql】主從複製MySql
- MySQL主從複製MySql
- mysql 主從複製搭建詳細步驟MySql
- MySQL-主從複製之搭建主資料庫MySql資料庫
- MySQL主從複製、半同步複製和主主複製概述MySql
- MySQL-主從複製之搭建從資料庫MySql資料庫
- MYSQL5.6.40原始碼安裝 主從搭建 主主搭建MySql原始碼
- MySQL主從複製_複製過濾MySql
- Docker Redis映象 主從複製DockerRedis
- MySQL 5.5 主主複製搭建流程MySql