基於docker的MySQL主從複製

1,環境

在linux環境上搭建html

2,版本

dockermysql

mysql:linux

wget https://dev.mysql.com/get/downloads/mysql-5.7.22-1.el7.i686.rpm-bundle.tarsql

3,安裝mysql

cd /usr/local安裝路徑docker

wget https://dev.mysql.com/get/downloads/mysql-5.7.22-1.el7.i686.rpm-bundle.tarjson

tar -zxvf  mysql-5.7.22-1.el7.i686.rpm-bundle.tarcentos

4,安裝docker

docker的安裝使用:bash

1. 安裝dockerless

   # yum install -y dockersocket

2. 啓動docker

   # systemctl start docker

3. 安裝docker鏡像

   # docker pull [docker鏡像地址]

   使用阿里雲鏡像

   修改/etc/docker/daemon.json

   {

    "registry-mirrors": ["https://uulfe4bd.mirror.aliyuncs.com"]

}

 4. 修改完畢重啓docker

   https://dev.aliyun.com/search.html 查詢鏡像文件。

   # systemctl restart docker

   5,獲取docker鏡像

mysql安裝須要創建在centos基礎上全部在,docker中須要安裝docker

   # docker pull https://registry.cn-hangzhou.aliyuncs.com/moensun/centos7

查看建立的centos7鏡像 docker images記錄ID 3db94df3e006

   6,建立docker容器

   # docker run -tid 3db94df3e006(查看centos7鏡像 時能夠看到)  /usr/sbin/init

重命名容器名稱

docker rename priceless_poitras mysql-master1

從新查看docker ps -a 容器名稱修改完成 

因爲是作主從複製,那麼應該建立兩個容器mysql-master1 和mysql-slave2這裏在建立mysql-slave2容器

(因爲以前建立了mysql-slave1,此次就建立了mysql-slave2,名稱本身命名都行。此次只是想寫個博客,方便之後學習,也方便想學習的同窗,一主mysql-master1,一從mysql-slave2,後面搭建好的話,就能夠根據本身需求搭建主主,多主多從了)

7,MySQL文件拷貝

Docker文件拷貝(容器<=>docker)

# docker cp 原路徑 docker容器路徑(系統=》docker)

# docker cp MySQL-server-5.6.38-1.el7.x86_64.rpm mysql_master2:/root/

# docker cp docker容器路徑 原路徑(docker=》系統)

將以前mysql解壓後的文件分別拷貝到mysql-master1和mysql-slave2
 docker cp mysql-community-common-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-client-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-server-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-server-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/
docker cp mysql-community-client-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/
docker cp mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/
docker cp mysql-community-common-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/

8,進入容器

# docker exec -it 容器名稱 /bin/bash

# docker exec -it mysql_master1 /bin/bash

開啓兩個遠程登陸窗口,分別進去主容器和從容器

主,docker exec -it mysql_master1 /bin/bash

從,docker exec -it mysql_slave2/bin/bash

9,MySQL主從複製

cd 到根目錄

ll查看複製的mysql的rpm

安裝mysql,安裝mysql須要安裝mysql依賴

(1)安裝centos系統安裝mysql依賴:

1.     perl perl-Module-Build

2.     autoconf

3.     libaio

4.     namuctl-libs

5.     net-tools

 yum install -y perl perl-Module-Build net-tools autoconf libaio numactl-libs

安裝mysql

(2)在根目錄下

rpm -ivh mysql-community-*.rpm

啓動mysql

systemctl  start mysqld

個人通常都會報錯 看日誌是磁盤沒有分配大小

須要vi     /etc/my.cnf中添加innodb_buffer_pool_size = 8M

若是沒有報錯最好

 

第一次啓動後會有個初始化的過程,會產生root帳戶的隨機密碼。

cat /var/log/mysqld.log  | grep password

鏈接mysql 

mysql -uroot -pkzl\(kVo-w0lg

其中(須要轉義

設置從新設置密碼

注意:若是隻想設置簡單密碼須要修改兩個全局參數:

mysql> set global validate_password_length=1;set global validate_password_policy=0;

set password = password(‘你的密碼’)

set password = password(‘123456’)

遠程登錄受權

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

mysql> flush privileges;

 

主從複製過程

(在主機上操做):

vi /etc/my.cnf

配置文件my.cnf

[client]

port                        = 3306

socket                      = /var/lib/mysql/mysql.sock

[mysqld]

#不管主機仍是從機server-id不能相同

server-id                    = 2

port                        = 3306

##原先有的話就能夠去掉

datadir                      = /var/lib/mysql

socket                       = /var/lib/mysql/mysql.sock

default-storage-engine          = InnoDB

log-bin                      = mysql-bin

log-bin-index                 = mysql-bin.index

relay-log                    = mysql-relay

relay-log-index               = mysql-relay.index

expire-logs-days              = 10

max-binlog-size              = 100M

max_binlog_cache_size        = 8M

log-slave-updates             = 1

binlog_cache_size            = 4M

# use MIXED binlog

binlog_format               = MIXED

#binlog_format              = ROW

#replicate-do-db             = db%.%

#replicate-ignore-db          = mysql.%

# ignore tables

replicate-wild-ignore-table     = mysql.%

sync_binlog                 = 1

relay_log_recovery           = 1

log_slave_updates           = 1

skip-name-resolve

sql_mode=STRICT_TRANS_TABLES

[mysqldump]

quick

max_allowed_packet = 32M

1.        建立同步複製的用戶

    mysql> create user 'repl'@'172.17.0.%' identified by '123456';

2.        給同步複製用戶賦權

    mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by '123456';

mysql> flush privileges;

3.        開啓binlog

    配置時候注意幾個坑:

    Replication-do-db的坑,若是多個庫則使用多行Replication-do-db進行配置

Replication-ignore-db的坑,若是忽略多個庫則使用多行Replication-ignore-db進行配置

4.        重啓mysql

#systemctl restart mysqld

5.       鏈接mysql

show master status \G;

 

從機操做:

vi /etc/my.cnf

配置文件my.cnf

[client]

port                        = 3306

socket                      = /var/lib/mysql/mysql.sock

[mysqld]

#不管主機仍是從機server-id不能相同

server-id                    = 20

port                        = 3306

##原先有的話就能夠去掉

datadir                      = /var/lib/mysql

socket                       = /var/lib/mysql/mysql.sock

default-storage-engine          = InnoDB

log-bin                      = mysql-bin

log-bin-index                 = mysql-bin.index

relay-log                    = mysql-relay

relay-log-index               = mysql-relay.index

expire-logs-days              = 10

max-binlog-size              = 100M

max_binlog_cache_size        = 8M

log-slave-updates             = 1

binlog_cache_size            = 4M

# use MIXED binlog

binlog_format               = MIXED

#binlog_format              = ROW

#replicate-do-db             = db%.%

#replicate-ignore-db          = mysql.%

# ignore tables

replicate-wild-ignore-table     = mysql.%

sync_binlog                 = 1

relay_log_recovery           = 1

log_slave_updates           = 1

skip-name-resolve

sql_mode=STRICT_TRANS_TABLES

[mysqldump]

quick

max_allowed_packet = 32M

鏈接mysql操做:

1. Stop slave;

主從複製的最關鍵語句:

2. Change master to

         Master_host=’172.17.0.2’,

         Master_user=’repl’,

         Master_password=’123456’,

         Master_log_file=’mysql-bin.000001’,

         Master_log_pos=120;

3. Start slave;

4.show slave status \G;

測試

 主mysql:create database db1;

 

從 mysql> show databases;

從mysql上建立了db1

相關文章
相關標籤/搜索