异步复制一主一从架构
环境架构
部署操作
#db01操作:
[root@db01 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
server-id=1
log-bin=/log/mysqlbin
read-only=0
binlog_format=ROW
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#db02操作
[root@db02 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
server-id=2
log-bin=/log/mysqlbin
relay-log=mysql-relay
binlog_format=ROW
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#db01操作
[root@db01 ~]# mysql -uroot -p1234
mysql> show variables like '%password%'
-> ;
+-------------------------------------------------+-----------------+
| Variable_name | Value |
+-------------------------------------------------+-----------------+
| caching_sha2_password_auto_generate_rsa_keys | ON |
| caching_sha2_password_digest_rounds | 5000 |
| caching_sha2_password_private_key_path | private_key.pem |
| caching_sha2_password_public_key_path | public_key.pem |
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| generated_random_password_length | 20 |
| mysql_native_password_proxy_users | OFF |
| password_history | 0 |
| password_require_current | OFF |
| password_reuse_interval | 0 |
| report_password | |
| sha256_password_auto_generate_rsa_keys | ON |
| sha256_password_private_key_path | private_key.pem |
| sha256_password_proxy_users | OFF |
| sha256_password_public_key_path | public_key.pem |
| validate_password.changed_characters_percentage | 0 |
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+-------------------------------------------------+-----------------+
24 rows in set (0.23 sec)
#修改密码规范严格性
mysql> set global validate_password.length=4;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.policy=LOW;
Query OK, 0 rows affected (0.00 sec)
#创建slave1用户
mysql> create user 'slave1'@'%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
#将replication salve授权给刚创建的用户
mysql> grant replication slave on *.* to 'slave1'@'%';
Query OK, 0 rows affected (0.00 sec)
#修改该用户信息,使他可以使用mysql_native_password
mysql> alter user 'slave1'@'%' identified with mysql_native_password by '1234';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysqlbin.000002 | 1145 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#db02操作
[root@db02 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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>
#使用主机刚创建授权的用户连接到主机指定的binlog日志和position
mysql> change master to master_host='192.168.10.16', master_user='slave1',master_password='1234',master_log_file='mysqlbin.000002',master_log_pos=1145;
Query OK, 0 rows affected, 8 warnings (0.12 sec)
#开启从机
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
#查看从机状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.10.16
Master_User: slave1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlbin.000002
Read_Master_Log_Pos: 1145
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 325
Relay_Master_Log_File: mysqlbin.000002
Slave_IO_Running: Yes # 从机的IO线程正在运行中
Slave_SQL_Running: Yes #从机的SQL线程正在运行中,这两个线程正常运行,一般就表示正常,成功了
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: 1145
Relay_Log_Space: 531
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: 1
Master_UUID: 39a78029-4311-11ee-987d-000c29124967
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.01 sec)
ERROR:
No query specified
#测试
#主机创库和表,查看从机是否也有
#主机:
mysql> create database test1;
Query OK, 1 row affected (0.01 sec)
mysql> use test1;
Database changed
mysql> create table employees(id int,name varchar(15),age int);
Query OK, 0 rows affected (0.39 sec)
mysql> insert into employees values(1,'小明',18);
Query OK, 1 row affected (0.06 sec)
mysql> select * from employees;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | 小明 | 18 |
+------+--------+------+
1 row in set (0.00 sec)
#从机
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test1;
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> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| employees |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from employees;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | 小明 | 18 |
+------+--------+------+
1 row in set (0.00 sec)
半同步复制一主一从架构
基于上面搭建好的主从架构,改为半同步复制的主从架构
默认情况下,搭建主从架构是异步复制的特性,不能保证数据的一致性,半同步复制性需要借助插件,虽然不能保证百分百的一致性,但是相比异步复制的一致性要高,当然,缺点就是损耗资源,需要等待ACK。网上还有一种说法是完全同步复制性,这种我感觉就是把半同步复制的count的参数,默认为1,改为了你从机的数量而已。没太大了解。还有一种就是组同步性,这个机制就是读写分离的情况下,然后放任读,管理写的线程,然后就是当写入的线程的ack回复过半的时候,就会提交事务。感觉相比前三个要复杂一些,毕竟还要基于mycat插件进行读写分离,这环境都分的这么细,性能也不会差哪里吧,还是获得图灵奖的人提出的。
mysql8.0.26之前用的是semisync_master.so 和 semisync_slave.so插件部署半同步
mysql8.0.26以后可以使用semisync_source.so 和 semisync_replica.so插件部署半同步,当然上面那个也可以。这里就使用source这种,用点不一样的。
db01:主机操作
[root@db01 ~]# mysql -uroot -p
mysql>
mysql> install plugin rpl_semi_sync_source soname 'semisync_source.so'; #下载插件
Query OK, 0 rows affected (0.15 sec)
mysql> show plugins; #展示本地所有插件
+----------------------------------+----------+--------------------+--------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------------+----------+--------------------+--------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| caching_sha2_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha2_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |
| daemon_keyring_proxy_plugin | ACTIVE | DAEMON | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_VIRTUAL | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CACHED_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SESSION_TEMP_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
| TempTable | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| ndbcluster | DISABLED | STORAGE ENGINE | NULL | GPL |
| ndbinfo | DISABLED | STORAGE ENGINE | NULL | GPL |
| ndb_transid_mysql_connection_map | DISABLED | INFORMATION SCHEMA | NULL | GPL |
| ngram | ACTIVE | FTPARSER | NULL | GPL |
| mysqlx_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |
| mysqlx | ACTIVE | DAEMON | NULL | GPL |
| rpl_semi_sync_source | ACTIVE | REPLICATION | semisync_source.so | GPL |
+----------------------------------+----------+--------------------+--------------------+---------+
49 rows in set (0.00 sec)
mysql> set global rpl_semi_sync_source_enabled = 1; #这里只能算是临时开启,还要在配置文件中写入
Query OK, 0 rows affected (0.00 sec)
[root@db01 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
server-id=1
log-bin=/log/mysqlbin
read-only=0
binlog_format=ROW
rpl_semi_sync_source_enabled=1 #永久开启插件
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
mysql> show status like 'rpl_semi_sync_source_status' #查看插件是否开启
-> ;
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Rpl_semi_sync_source_status | ON |
+-----------------------------+-------+
1 row in set (0.00 sec)
db02:从机操作
[root@db02 ~]# mysql -uroot -p
Enter password:
mysql>
mysql> install plugin rpl_semi_sync_replica soname 'semisync_replica.so'; #下载从机的插件
Query OK, 0 rows affected (0.16 sec)
mysql> set global rpl_semi_sync_replica_enabled=1; #临时开启插件
Query OK, 0 rows affected (0.00 sec)
[root@db02 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
server-id=2
log-bin=/log/mysqlbin
relay-log=mysql-relay
binlog_format=ROW
rpl_semi_sync_replica_enabled=1 #永久开启插件
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
mysql> stop slave io_thread; #关闭一下io线程,因为默认用的异步复制,所以要重启一下
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> start slave io_thread;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show status like 'rpl_semi_sync_replica_status' #查看插件状态
-> ;
+------------------------------+-------+
| Variable_name | Value |
+------------------------------+-------+
| Rpl_semi_sync_replica_status | ON |
+------------------------------+-------+
1 row in set (0.00 sec)
db01:主机操作
[root@db01 ~]# mysql -uroot -p
mysql>
mysql> show variables like 'rpl_semi_sync%'; #查看插件的环境变量配置
+---------------------------------------------+------------+
| Variable_name | Value |
+---------------------------------------------+------------+
| rpl_semi_sync_source_enabled | ON | #开启
| rpl_semi_sync_source_timeout | 10000 | #等待ACK超时时间
| rpl_semi_sync_source_trace_level | 32 | #级别
| rpl_semi_sync_source_wait_for_replica_count | 1 | #等待的从机数量
| rpl_semi_sync_source_wait_no_replica | ON | #从机是否开启
| rpl_semi_sync_source_wait_point | AFTER_SYNC | #选项
+---------------------------------------------+------------+
6 rows in set (0.00 sec)
mysql> show status like 'rpl_semi_sync%' #查看插件的状态,注意下面的参数会发生变化
-> ;
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_source_clients | 1 |
| Rpl_semi_sync_source_net_avg_wait_time | 0 |
| Rpl_semi_sync_source_net_wait_time | 0 |
| Rpl_semi_sync_source_net_waits | 0 |
| Rpl_semi_sync_source_no_times | 0 |
| Rpl_semi_sync_source_no_tx | 0 |
| Rpl_semi_sync_source_status | ON |
| Rpl_semi_sync_source_timefunc_failures | 0 |
| Rpl_semi_sync_source_tx_avg_wait_time | 0 |
| Rpl_semi_sync_source_tx_wait_time | 0 |
| Rpl_semi_sync_source_tx_waits | 0 |
| Rpl_semi_sync_source_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_source_wait_sessions | 0 |
| Rpl_semi_sync_source_yes_tx | 0 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
mysql> show databases; #下面进行操作,测试我们的机制是否切换到了同步复制
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.01 sec)
mysql> use test1;
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> select * from employees;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | 小明 | 18 |
+------+--------+------+
1 row in set (0.00 sec)
mysql> insert into employees values(2,'小芳',20); #插入两条数据,
Query OK, 1 row affected (0.00 sec)
mysql> insert into employees values(2,'小王',23);
Query OK, 1 row affected (0.00 sec)
mysql> show status like 'rpl_semi_sync%'; #由于上面插入了两条数据,则从机就要发送两次ack,主机也要等待两次
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_source_clients | 1 |
| Rpl_semi_sync_source_net_avg_wait_time | 0 |
| Rpl_semi_sync_source_net_wait_time | 0 |
| Rpl_semi_sync_source_net_waits | 2 | # 主库等待从库回复的总次数。
| Rpl_semi_sync_source_no_times | 0 |
| Rpl_semi_sync_source_no_tx | 0 |
| Rpl_semi_sync_source_status | ON |
| Rpl_semi_sync_source_timefunc_failures | 0 |
| Rpl_semi_sync_source_tx_avg_wait_time | 574 | # 主机等待事务的平均时间
| Rpl_semi_sync_source_tx_wait_time | 1149 | # 主机等待事务的总时间
| Rpl_semi_sync_source_tx_waits | 2 | # 主机等待事务的总次数
| Rpl_semi_sync_source_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_source_wait_sessions | 0 |
| Rpl_semi_sync_source_yes_tx | 2 | # 从库成功确认的事务数。
+--------------------------------------------+-------+
14 rows in set (0.00 sec)