Docker Desktop
https://www.docker.com/get-started,这个是有图形界面的Docker,可以省去很多命令操作,对系统有一定要求;docker pull mysql:5.7
Dockerfile
和My.cnf
Dockerfile
– 打包配置文件My.cnf
– Mysql配置文件,这里修改好以后替换Mysql原有文件,也可以打包以后进入容器修改,但是Docker原生没有Vim编辑,需要更新源安装,但是速度很慢;Dockerfile
FROM mysql:5.7
COPY my.cnf /etc/mysql/
EXPOSE 3306
CMD ["mysqld"]
mysql:5.7
– 不写版本号5.7会拉取最新版Mysql,最新版的授权有变化,需修改授权方式My.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# 新添加的在下面
#启用二进制,必须
log-bin=mysql-bin
#填写整数,每个数据库不同
server-id=1
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# 新添加的在下面
#启用二进制,必须
log-bin=mysql-bin
#填写整数,每个数据库不同
server-id=2
server-id=1 #任意自然数n,只要保证两台MySQL主机不重复就可以了。
log-bin=mysql-bin #开启二进制日志
auto_increment_increment=2 #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=1 #起始值。一般填第n台主MySQL。此时为第一台主MySQL
binlog-ignore=mysql #忽略mysql库【我一般都不写】
binlog-ignore=information_schema #忽略information_schema库【我一般都不写】
replicate-do-db=aa #要同步的数据库,不填默认所有库
# 配置好后重启MySQL
# https://www.cnblogs.com/phpstudy2015-6/p/6485819.html#_label7
docker build -t master/mysql .
docker build -t slave/mysql .
.
– 点代表本文件夹下所有,必写
查看是否成功
docker run -p 17717:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 -d master/mysql
docker run -p 17718:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=123456 -d slave/mysql
>-
按钮进入容器内部//点击`>-`按钮相当于命令行执行,操作2选一
docker exec -it mysql-slave bash
mysql -uroot -p
//然后输入密码123456
mysql>GRANT REPLICATION SLAVE ON *.* TO 'user'@'192.168.2.14' IDENTIFIED BY '123456';(指定ip)
或者
mysql>GRANT REPLICATION SLAVE ON *.* to 'user'@'%' identified by '123456';(所有ip)
'user'@'192.168.99.100'
– user是指定从数据库用户名,ip是从服务器主机ip,这里就是本机局域网ip123456
– Mysql登陆密码mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 137 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
File
,Position
和主数据库对外端口
Port:17717 是关键值,需要记录>-
按钮进入从数据库内部mysql> change master to
master_host='192.168.2.14',
master_user='user',
master_log_file='mysql-bin.000003',
master_log_pos=137,
master_port=17717,
master_password='123456';
master_host
– 主库IP地址,这里就是本机IPmaster_user
– 连结主库使用的用户名,就是刚才授权的用户名master_log_file
– 刚才需要记录的主库的File值master_log_pos
– 刚次需要记录的主库的Position值master_port
– 主库的外端口master_password='123456';
– 主库密码,分号结束mysql> start slave;// 启动从服务器复制功能
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.14
Master_User: user
Master_Port: 32768
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1338
Relay_Log_File: 8d1e3b87d499-relay-bin.000002
Relay_Log_Pos: 1221
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
...
1 row in set (0.00 sec)
Slave_IO_Running
/ Slave_SQL_Running
– 都为Yes是表示启动成功,其他值都不成功Replicate_Do_DB
– 空表示所有库mysql> create database mytest;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| mytest |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mytest
Database changed
mysql> create table demo(id int(3),name varchar(10));
Query OK, 0 rows affected (0.04 sec)
mysql> insert into demo values(1,'uname');
Query OK, 1 row affected (0.01 sec)
mysql> select * from demo;
+------+-----------+
| id | name |
+------+-----------+
| 1 | uname |
+------+-----------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| mytest |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mytest
Database changed
mysql> select * from demo;
+------+-----------+
| id | name |
+------+-----------+
| 1 | uname |
+------+-----------+
1 row in set (0.00 sec)
-v
挂载的方式启动容器具体参考 https://www.cnblogs.com/konglingxi/p/14808850.html