主机情况
主机名 | ip | 用途 |
k8smaster01 | 192.168.23.100 | mysql主 |
k8smaster02 | 192.168.23.101 | mysql从 |
主从复制的原理
第一步:在每个更新数据的事物完成之前,主服务器都会把数据更改记录到二进制日志中。即使事物在执行期间是交错的,mysql也会串行地把事物写入到二进制日志中,写入完成之后,主服务器告诉存储引擎提交事物。
第二步:从服务器把主服务器的二进制日志拷贝到自己的硬盘,即"中继日志"中。首先,它启动一个工作线程,叫I/O从线程。这个I/O线程开启一个普通的客户端连接,然后启动一个特殊的二进制日志存储进程(binlog dump)进程。这个转储进程从主服务器的二进制日志中读取事件,它不会对事物进行轮询。如果它跟上了主服务器,就会进入休眠状态,并等待有新的事件发生时主服务器发出的信号,I/O线程把事件写入到从服务器的中继日志中。
第三步:SQL从线程读取中继日志,并且重放其中的事件,然后更新从服务器的数据。由于这个进程能跟上I/O线程,中继日志通常都在操作系统的缓存中,所有中继日志的开销很低。SQL从线程执行的事件也可以被写入从服务器自己的二进制日志中或是不写。
导出数据备份【锁表后备份防止数据再更新】
[zhaiky@k8smaster01 bin]$ ./mysqldump -R -E -h192.168.23.100 -uprd -p -P3306 -S /tmp/mysql.sock --databases prd >prd.sql
Enter password:
[zhaiky@k8smaster01 bin]$
my.cnf配置
主服务my.cnf配置
[zhaiky@k8smaster01 config]$ more my.cnf
[mysqld]
basedir=/data/mysql/mysql
datadir=/data/mysql/mysql/data
socket= /var/lib/mysql/mysql.sock
user=zhaiky
port=3306
event_scheduler=1 #开启计划任务
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections=2000
character_set_server=utf8
slow_query_log=1
slow_query_log_file=/data/mysql/mysql/mysql_slow.log
innodb_buffer_pool_size=200M
max_connect_errors=100
log-bin=mysql-bin
binlog_format=mixed
skip-host-cache
skip-name-resolve
explicit_defaults_for_timestamp
lower_case_table_names=1
server_id=100
auto-increment-offset=1 #主从配置不同点
auto-increment-increment=2
#mysql中有自增长字段,在做数据库的主主同步时需要设置自增长的两个相关配置:auto_increment_offset和auto_increment_increment。
#auto_increment_offset表示自增长字段从那个数开始,他的取值范围是1 .. 65535
#auto_increment_increment表示自增长字段每次递增的量,其默认值是1,取值范围是1 .. 65535
#在主主同步配置时,需要将两台服务器的auto_increment_increment增长量都配置为2,而要把auto_increment_offset分别配置为1和2,可以避免两台服务器同时做更新时自增长字段的值之间发生冲突。
replicate-ignore-db=information_schema
relay-log=mysqld-relay-bin
[mysqld_safe]
log-error=/data/mysql/mysql/mysqld.log
pid-file=/data/mysql/mysql/mysqld.pid
[zhaiky@k8smaster01 config]$
从服务my.cnf配置
[zhaiky@k8smaster02 config]$ more my.cnf
[mysqld]
basedir=/data/mysql/mysql
datadir=/data/mysql/mysql/data
socket= /var/lib/mysql/mysql.sock
user=zhaiky
port=3306
event_scheduler=1
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections=2000
character_set_server=utf8
slow_query_log=1
slow_query_log_file=/data/mysql/mysql/mysql_slow.log
innodb_buffer_pool_size=200M
max_connect_errors=100
log-bin=mysql-bin
binlog_format=mixed
skip-host-cache
skip-name-resolve
explicit_defaults_for_timestamp
lower_case_table_names=1
server_id=101
auto-increment-offset=2
auto-increment-increment=2
replicate-ignore-db=information_schema
relay-log=mysqld-relay-bin
[mysqld_safe]
log-error=/data/mysql/mysql/mysqld.log
pid-file=/data/mysql/mysql/mysqld.pid
[zhaiky@k8smaster02 config]$
主从复制配置
1)开启log-bin日志
mysql> show variables like 'log_bin'; #如果value的值为off,在my.cnf的[mysqld]的配置中添加log-bin=mysql-bin
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0.01 sec)
2)更改mysql服务器的server id,且server id要唯一,修改需要重启
3)建立仅限slave主机使用的专门用于进行复制数据的用户
在主服务操作
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 'prd'@'%' IDENTIFIED BY '123456'; #创建prd同步用户
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'prd'@'%' identified by '123456' with grant option; #为同步用户设置权限
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges; #权限生效
Query OK, 0 rows affected (0.00 sec)
mysql> select user,authentication_string,host from user; #查看创建的用户
+---------------+-------------------------------------------+-----------+
| user | authentication_string | host |
+---------------+-------------------------------------------+-----------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | % |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| zhaiky | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | % |
| prd | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | % |
+---------------+-------------------------------------------+-----------+
5 rows in set (0.00 sec)
mysql> flush tables with read lock; #设置读锁,以防数据更新
Query OK, 0 rows affected (0.00 sec)
mysql> show master status; #查看主状态
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 869 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
从节点配置
mysql> change master to master_host='192.168.23.100',master_user='prd',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=869;
master_host:主的mysql地址,我们这里是发布主机
master_user:验证用户
master_password:验证密码
master_log_file:show master status中File
master_log_pos:show master status中Position
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave; #从数据库启用
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G #从数据库查看slave状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.23.100
Master_User: prd
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 869
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #表示配置成功
Replicate_Do_DB:
Replicate_Ignore_DB: information_schema
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: 869
Relay_Log_Space: 528
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: 100
Master_UUID: 93c20797-5c4e-11ea-9b82-000c291c4997
Master_Info_File: /data/mysql/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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:
1 row in set (0.00 sec)
mysql>
4)主服务创建数据库,验证是否同步从
主服务器创建数据和插入数据
mysql> unlock tables; #解锁
Query OK, 0 rows affected (0.00 sec)
mysql> create database if not exists prd default charset utf8 collate utf8_general_ci; #创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> use prd; #切换数据库到prd
Database changed
mysql> create table test(
-> id int not null auto_increment,
-> username varchar(100) not null,
-> createdate date,
-> primary key (id)); #创建表
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO `prd`.`test` (`username`, `createdate`) VALUES ('zhaiky', '2020-03-05'); #插入数据
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO `prd`.`test` (`username`, `createdate`) VALUES ('zhaiky', '2020-03-05');#插入数据
Query OK, 1 row affected (0.00 sec)
mysql>
从服务查看prd数据库是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| prd |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use prd; #切换数据库
Database changed
mysql> select * from test;#查询数据
+----+----------+------------+
| id | username | createdate |
+----+----------+------------+
| 1 | zhaiky | 2020-03-05 |
| 3 | zhaiky | 2020-03-05 |
+----+----------+------------+
2 rows in set (0.00 sec)
mysql>