docker 安裝mysql5.7 主備過程

1.修改鏡像的默認存儲位置

vim /etc/systemd/system/multi-user.target.wants/docker.service

設置路徑
ExecStart=/usr/bin/dockerd --graph=/mnt/lib/docker

2.設置鏡像文件

獲取阿里雲鏡像地址 

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

3.docker info 查看

[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.12.1-ce
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9b55aab90508bd389d7654c4baf173a981477d55
runc version: 9f9c96235cc97674e935002fc3d78361b696a69e
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-514.10.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.797GiB
Name: localhost.localdomain
ID: IZPN:OTU5:PBB5:TVTC:C7N5:7LJC:MJBI:RVG4:57G6:ZXOF:EVSP:43CJ
Docker Root Dir: /mnt/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:
 https://xxx.mirror.aliyuncs.com/
Live Restore Enabled: false

4.主數據庫配置

修改配置文件並重啓

#vi /etc/mysql/my.cnf
[mysqld]
log-bin=mysql-bin //[必須]啓用二進制日誌
server-id=1 //[必須]服務器唯一ID,默認是1

創建遠程備份用戶

CREATE USER 'repl'@'從ip' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'從ip';
FLUSH PRIVILEGES;

備份數據

mysql> FLUSH TABLES WITH READ LOCK;

mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 1529881 | openser | mysql,test |
+------------------+----------+--------------+------------------+
(重啓導出數據端口:mysqldump -u root -p123456 --opt -R openser > openser20121203.sql)

mysql> UNLOCK TABLES;

5.docker啓動從數據庫

docker run --name slave -e MYSQL_ROOT_PASSWORD=123456 -p 9002:3306 -d mysql:5.7

同步已有數據

docker容器複製到本地 格式:

docker cp container_id:<docker容器內的路徑> <本地保存文件的路徑>

本地複製文件到docker容器 格式:

docker cp 本地文件的路徑 container_id:<docker容器內的路徑>

進入容器

docker exec -it slave bash

docker鏡像安裝vim命令

apt-get update

apt-get install vim

#vim /etc/mysql/my.cnf
[mysqld]
log-bin=mysql-bin //[必須]啓用二進制日誌
server-id=2 //[必須]服務器唯一ID,默認是1,從庫設置爲2

#stop slave
#reset slave

change master to master_host='172.16.3.14',master_port=3306,master_user='repl',master_password='123456',master_log_file=' mysql-bin.000001',master_log_pos=3098;

start slave


show slave status\G
確認以下兩個值爲YES

***************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

**************

 

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章