赞
踩
拉取mysql镜像到本地
docker pull mysql
创建容器并且配置文件映射关系
创建映射文件:
mkdir /usr/local/software/mysql cd /usr/local/software/mysql mkdir 3306/conf 3306/data mkdir 3310/conf 3310/data mkdir 3311/conf 3311/data
- 1
- 2
- 3
- 4
- 5
配置
/conf
目录下的my.cnf文件[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL default_authentication_plugin=mysql_native_password # Custom config should go here !includedir /etc/mysql/conf.d/ server-id=200 log_bin=master-bin binlog_format=row
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
创建容器并且运行:
# 这里注意docker容器里面的mysql的my.cnf的位置,在第10行/etc/my.cnf,或者/etc/mysql/my.cnf # 建议自己先创建一个mysql容器进入docker里面的/etc、/etc/mysql查看是否有my.cnf文件,这里是在/etc下 docker run -it \ --name mysql_3306 \ --privileged \ -p 3306:3306 \ --network wn_docker_net \ --ip 172.18.12.2 \ -v /usr/local/software/mysql/3306/conf/my.cnf:/etc/my.cnf \ -v /usr/local/software/mysql/3306/data:/var/lib/mysql \ -v /usr/local/software/mysql/3306/mysql-files:/var/lib/mysql-files \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
进入容器进行配置主服务器
docker exec -it mysql_3306 bash mysql -uroot -p123456 # 创建与从服务器进行通信的用户 create user 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%'; flush privileges; # 查看主服务器(master)的状态 show master status; # 拿到第一列master-bin后面的数字和第二列的数字 # 例: master-bin.000003 | 840 | # 拿到创建时指定的ip:172.18.12.2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
配置从服务器
# 同2.* # 1. 配置 slave01(3310)的 my.cnf文件 ... # 这里不同 server-id=201 log-bin=slave-01-bin relay_log=mysql-relay-bin read-only=1 # 配置 slave02(3311)的 my.cnf文件 ... # 这里不同 server-id=202 log-bin=slave-02-bin relay_log=mysql-relay-bin read-only=1 # 2. 创建容器(3310) docker run -it \ --name mysql_3310 \ --privileged \ -p 3310:3306 \ --network wn_docker_net \ --ip 172.18.12.3 \ -v /usr/local/software/mysql/3310/conf/my.cnf:/etc/my.cnf \ -v /usr/local/software/mysql/3310/data:/var/lib/mysql \ -v /usr/local/software/mysql/3310/mysql-files:/var/lib/mysql-files \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql # 创建容器(3311) docker run -it \ --name mysql_3311 \ --privileged \ -p 3311:3306 \ --network wn_docker_net \ --ip 172.18.12.4 \ -v /usr/local/software/mysql/3311/conf/my.cnf:/etc/my.cnf \ -v /usr/local/software/mysql/3311/data:/var/lib/mysql \ -v /usr/local/software/mysql/3311/mysql-files:/var/lib/mysql-files \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
将从服务器(slave)与主服务器(master)关联
# 进入salve01(3310) docker exec -it mysql_3310 bash mysql -uroot -p123456 # 关联master,看master的参数,ip master-bin position change master to master_host='172.18.12.2', master_user='slave',master_password='123456',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=840; # 开启slave start slave; # 查看salve状态 show slave status \G # 找到这两个参数,后面为yes即可 # Slave_IO_Running: Yes # Slave_SQL_Running: Yes # salve02(3011)同理
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
从服务器(slave)的权限为只读,所以需要创建slave的只读账号
# 进入mysql里面,以后登录就用这个账号而不是root create user 'suser'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; GRANT SELECT ON *.* TO 'suser'@'%'; flush privileges;
- 1
- 2
- 3
- 4
测试:slave都登录只读账号,然后在master创建数据库,刷新查看slave里面有没有
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。