/mysql-master/conf
#把 MYSQL_ROOT_PASSWORD=12346 改成 MYSQL_ALLOW_EMPTY_PASSWORD=yes
#MYSQL_ALLOW_EMPTY_PASSWORD=yes 允许容器以空白密码登录root用户
docker run -d --name mysql-master -p 3306:3306 --privileged=true -v /mysql-master/log:/var/log/mysql -v /mysql-master/data:/var/lib/mysql -v /mysql-master/conf:/etc/mysql -v /mysql-master/mysql-files:/var/lib/mysql-files -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql
注意:启动时如果需要挂载外部文件,那么一定要将/var/lib/mysql-files这个内部文件也挂载到外部,否则启动容器后会自动停止,查看容器信息如下报错
[root@localhost conf]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5889997cc277 mysql "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-master
[root@localhost conf]# docker exec -it 5889997cc277 /bin/bash
root@5889997cc277:/#
#不需要填写密码
root@5889997cc277:/# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
#查看数据库
show databases;
#切换数据库
use mysql;
#设置密码
#PASSWORD EXPIRE NEVER 密码永不过期
#mysql_native_password 加密插件
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456' PASSWORD EXPIRE NEVER;
#退出控制台
exit;
#登录
mysql -uroot -p123456
这个步骤可以在创建容器前直接将改好的内容放入即将挂载的配置文件路径中。就可以省略后面的重启了
[root@localhost /]# cd /mysql-master/conf
[root@localhost conf]# vim my.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
# Custom config should go here
!includedir /etc/mysql/conf.d/
[root@localhost conf]# docker restart 63026a9fbbd0
[root@localhost conf]# docker exec -it 4be5f90c63f7 /bin/bash
root@4be5f90c63f7:/etc/mysql# mysql -uroot -p123456
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create user 'slave'@'%'identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave,replication client on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql>
从安装过的容器中将配置文件整个路径复制到宿主机中
docker cp [容器ID]:/etc/mysql /mysql-slave/conf
将容器的/etc/mysql 路径下的内容复制到宿主机的/mysql-slave/conf中
[root@localhost conf]# docker run -d --name mysql-slave -p 3307:3307 --privileged=true -v /mysql-slave/log:/var/log/mysql -v /mysql-slave/data:/var/lib/mysql -v /mysql-slave/conf:/etc/mysql -v /mysql-slave/mysql-files:/var/lib/mysql-files -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql
[root@localhost conf]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4188fd4dc9c mysql "docker-entrypoint.s…" 8 seconds ago Up 4 seconds 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp, :::3307->3307/tcp mysql-slave
4be5f90c63f7 mysql "docker-entrypoint.s…" 22 minutes ago Up 15 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-master
[root@localhost conf]# docker exec -it c4188fd4dc9c /bin/bash
root@c4188fd4dc9c:/# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456' PASSWORD EXPIRE NEVER;
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
Bye
mysql> exit;
Bye
root@c4188fd4dc9c:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
这个步骤可以在创建容器前直接将改好的内容放入即将挂载的配置文件路径中。就可以省略后面的重启了
[mysqld]
#pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
## relay_log配置中继日志
relay_log=mysql-relay-bin
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
## slave设置为只读(具有super权限的用户除外)
read_only=1
!includedir /etc/mysql/conf.d/
docker restart [容器名]
mysql> show master status ;
±----------------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±----------------------±---------±-------------±-----------------±------------------+
| mall-mysql-bin.000001 | 156 | | mysql | |
±----------------------±---------±-------------±-----------------±------------------+
1 row in set (0.01 sec)
mysql>
master_host:主数据库的IP地址;
master_port:主数据库的运行端口;
master_user:在主数据库创建的用于同步数据的用户账号;
master_password:在主数据库创建的用于同步数据的用户密码;
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
master_connect_retry:连接失败重试的时间间隔,单位为秒。
change master to master_host='192.168.109.141',master_user='slave',master_password='123456',master_port=3306,master_log_file='mysql-bin.000003',master_log_pos=156;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.109.141
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 156
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: no
Slave_SQL_Running: no
Replicate_Do_DB:
Replicate_Ignore_DB:
start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.109.141
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 156
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
主节点创建库创建表,从节点可以同步。
【报错如下】:
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 30 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
【原因】:mysql登录验证的默认插件变更了,从mysql_native_password变为了caching_sha2_password
【解决方案】:将mysql的登录插件改回为mysql_native_password
1)master节点:修改root 和 slave用户
mysql> ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
2)slave节点:查询当前插件
mysql> use mysql;
Database changed
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
3)slave修改插件
root 用户的验证器插件为 caching_sha2_password 修改身份验证类型(修改密码)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
#查看修改后的插件
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | mysql_native_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
停止
mysql> stop slave;
重新配置主从关系
change master to master_host='192.168.109.141',master_user='slave',master_password='123456',master_port=3306,master_log_file='mysql-bin.000003',master_log_pos=156;
启动
mysql> start slave;
查看
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.109.141
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 156
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes