Ubuntu docker mysql 集群 1
设置一主多从
主/从 | 服务名 | IP | PORT | docker虚拟IP (此处没有创建自定义网络,后续再处理) |
---|---|---|---|---|
主1 | mysql8_master1000 | 192.168.79.177 | 13306 | .NetworkSettings.IPAddress 查看 |
从1 | mysql8_worker1001 | 192.168.79.177 | 13316 | .NetworkSettings.IPAddress 查看 |
从2 | mysql8_worker1002 | 192.168.79.177 | 13326 | .NetworkSettings.IPAddress 查看 |
# 创建主库相关目录
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_master1000
# 创建从库相关目录
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_worker1001
# 创建从库相关目录
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_worker1002
主库配置
my.cnf 配置,后续映射到docker容器中
# my.cnf
tester@server01:~$ cat > /home/tester/data/docker/mysql8_master1000/my.cnf <<EOF
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
!includedir /etc/mysql/conf.d/
default_authentication_plugin= mysql_native_password
# must unique value
server-id=1000
log-bin=mysql-bin
# 日志文件自动删除/过期的天数,避免占用磁盘空间。默认值为0-不自动删除。
expire_logs_days=14
# binlog日志写入后与硬盘同步,备注有说明具体配置含义
# sync_binlog=1
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
# STATEMENT模式(基于SQL语句的复制(statement-based replication, SBR))
# ROW模式(基于行的复制(row-based replication, RBR))
# MIXED模式(混合模式复制(mixed-based replication, MBR))
binlog_format=row
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
启动 mysql 主库
tester@server01:~$ docker run -d --name mysql8_master1000 \
--privileged=true \
-v /home/tester/data/docker/mysql8_master1000/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_master1000/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_master1000/data:/var/lib/mysql \
-p 13306:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
查看已运行的实例
tester@server01:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03568a1ad17c mysql "docker-entrypoint.s…" 6 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql8_master1000
# 查看docker ip 地址
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_master1000
172.17.0.4
安装客户端
tester@server01:~$ sudo apt install mysql-client
通过客户端验证是否能登录成功
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13306
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 8
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
# 记住这个Position value `157`, 后续会用到
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show slave status;
Empty set, 1 warning (0.00 sec)
mysql> show slave hosts;
Empty set, 1 warning (0.00 sec)
从库01配置
my.cnf 配置,后续映射到docker容器中
tester@server01:~$ cat > /home/tester/data/docker/mysql8_worker1001/my.cnf <<EOF
[mysqld]
server-id=1001
log-bin=mysql-worker-bin
relay_log=mysql-worker-relay-bin
log_bin_trust_function_creators=true
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
binlog_format=row
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
default_authentication_plugin= mysql_native_password
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
从库02配置
my.cnf 配置,后续映射到docker容器中
tester@server01:~$ cat > /home/tester/data/docker/mysql8_worker1002/my.cnf <<EOF
[mysqld]
server-id=1002
log-bin=mysql-worker-bin
relay_log=mysql-worker-relay-bin
log_bin_trust_function_creators=true
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
binlog_format=row
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
default_authentication_plugin= mysql_native_password
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
启动从库01
tester@server01:~$ docker run -d --name mysql8_worker1001 \
--privileged=true \
-v /home/tester/data/docker/mysql8_worker1001/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_worker1001/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_worker1001/data:/var/lib/mysql \
-p 13316:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_worker1001
172.17.0.2
启动从库02
tester@server01:~$ docker run -d --name mysql8_worker1002 \
--privileged=true \
-v /home/tester/data/docker/mysql8_worker1002/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_worker1002/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_worker1002/data:/var/lib/mysql \
-p 13326:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_worker1002
172.17.0.3
查看已运行的mysql镜像实例
tester@server01:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6a222ed8a0e mysql "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:13326->3306/tcp, :::13326->3306/tcp mysql8_worker1002
e94c89378877 mysql "docker-entrypoint.s…" 24 seconds ago Up 23 seconds 33060/tcp, 0.0.0.0:13316->3306/tcp, :::13316->3306/tcp mysql8_worker1001
03568a1ad17c mysql "docker-entrypoint.s…" 12 minutes ago Up 3 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql8_master1000
通过客户端查看是否连接成功
从库01修改master
### 从库01
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13316
mysql> show slave status;
Empty set, 1 warning (0.01 sec)
mysql> show slave hosts;
Empty set, 1 warning (0.00 sec)
# 修改 master
mysql> change master to master_host='172.17.0.4',master_user='root',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=157;
Query OK, 0 rows affected, 7 warnings (0.22 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.17 sec)
# 如果Slave_IO_Running: No Slave_SQL_Running: Yes 则查看server_id 是否唯一
# 则执行 stop slave, 重新 change master to ... 再次 start slave;
# mysql> stop slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 172.17.0.4
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 157
Relay_Log_File: mysql-worker-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 157
Relay_Log_Space: 543
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1000
Master_UUID: 633de84d-6a8a-11ed-9c24-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
# 以下是 show slave status \G 带了 ';' 会出现次错误
ERROR:
No query specified
mysql>
从库02修改master
### 从库02
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13326
mysql> show slave status;
Empty set, 1 warning (0.00 sec)
mysql> show slave hosts;
Empty set, 1 warning (0.00 sec)
mysql> change master to master_host='172.17.0.2',master_user='root',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=157;
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.17 sec)
# 如果Slave_IO_Running: No Slave_SQL_Running: Yes 则查看server_id 是否唯一
# 则执行 stop slave, 重新 change master to ... 再次 start slave;
# mysql> stop slave;
# 此处不带 ';'
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 172.17.0.4
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 157
Relay_Log_File: mysql-worker-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 157
Relay_Log_Space: 543
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1000
Master_UUID: 633de84d-6a8a-11ed-9c24-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
mysql>
查看主库
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 1002 | | 3306 | 1000 | 25f10e42-6a8c-11ed-9cb1-0242ac110003 |
| 1001 | | 3306 | 1000 | 1aa1be51-6a8c-11ed-9de9-0242ac110002 |
+-----------+------+------+-----------+--------------------------------------+
2 rows in set, 1 warning (0.00 sec)
主库创建新库,从库1从库2都会同步新库
# 主库创建 test, test01 库
mysql> create database test;
Query OK, 1 row affected (0.02 sec)
mysql> create database test01;
Query OK, 1 row affected (0.02 sec)
# 从库1从库2都会同步新库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| test01 |
+--------------------+
6 rows in set (0.00 sec)
备注
在主服务器上最重要的二进制日志设置是sync_binlog,这使得mysql在每次提交事务的时候把二进制日志的内容同步到磁盘上,即使服务器崩溃也会把事件写入日志中。
sync_binlog
不仅影响到 binlog 对MySQL所带来的性能损耗,同时也影响到MySQL中数据的完整性。
sync_binlog=0
,当事务提交之后,MySQL不做fsync之类的磁盘同步指令刷新binlog_cache中的信息到磁盘,而让Filesystem自行决定什么时候来做同步,或者cache满了之后才同步到磁盘。sync_binlog=n
,当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。- 在MySQL中系统默认的设置是
sync_binlog=0
,也就是不做任何强制性的磁盘刷新指令,这时候的性能是最好的,但是风险也是最大的。因为一旦系统Crash,在binlog_cache中的所有binlog信息都会被丢失。而当设置为“1”的时候,是最安全但是性能损耗最大的设置。因为当设置为1的时候,即使系统Crash,也最多丢失binlog_cache中未完成的一个事务,对实际数据没有任何实质性影响。 sync_binlog
值为 0/1 系统写入性能差距可能高达5倍甚至更多。
通常不会使用同一个数据库操作账号如以上root
账号处理同步日志,会在从库1从库2分别新建一个账号用于同步主库数据。
相关配置参数 如XXX 使用 show variables like '%XXX%'
查看;
mysql> show variables like '%binlog%';
+------------------------------------------------+----------------------+
| Variable_name | Value |
+------------------------------------------------+----------------------+
| binlog_cache_size | 4194304 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_encryption | OFF |
| binlog_error_action | ABORT_SERVER |
| binlog_expire_logs_auto_purge | ON |
| binlog_expire_logs_seconds | 2592000 |
| binlog_format | ROW |
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
| binlog_gtid_simple_recovery | ON |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| binlog_rotate_encryption_master_key_at_startup | OFF |
| binlog_row_event_max_size | 8192 |
| binlog_row_image | FULL |
| binlog_row_metadata | MINIMAL |
| binlog_row_value_options | |
| binlog_rows_query_log_events | OFF |
| binlog_stmt_cache_size | 32768 |
| binlog_transaction_compression | OFF |
| binlog_transaction_compression_level_zstd | 3 |
| binlog_transaction_dependency_history_size | 25000 |
| binlog_transaction_dependency_tracking | COMMIT_ORDER |
| innodb_api_enable_binlog | OFF |
| log_statements_unsafe_for_binlog | ON |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| sync_binlog | 1 |
+------------------------------------------------+----------------------+
30 rows in set (0.01 sec)
常见错误码
- 1007:数据库已存在,创建数据库失败
- 1008:数据库不存在,删除数据库失败
- 1050:数据表已存在,创建数据表失败
- 1051:数据表不存在,删除数据表失败
- 1054:字段不存在,或程序文件跟数据库有冲突
- 1060:字段重复,导致无法插入
- 1061:重复键名
- 1062 Error ‘Duplicate entry ‘X’ for key ‘PRIMARY’ on query
- 1068:定义了多个主键
- 1094:位置线程ID
- 1146:数据表缺失,请恢复数据库
- 1053:复制过程中主服务器宕机
- 1062:主键冲突 Duplicate entry ‘%s’ for key %d