前言
随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器。在此之前我们必须要保证每台MySQL服务器里的数据同步。数据同步我们可以通过MySQL内部配置就可以轻松完成,主要有主从复制和主主复制。
演示:实现mysql主从复制
配置主节点
1.修改主节点配置文件
...
#binlog54 #binlog_format =STATEMENT55 binlog_format =row <
57 log-bin = /data/mysql/mysql3306/logs/mysql-bin <
62 expire_logs_days = 10
63#procedure64 log_bin_trust_function_creators=1
65...
2.重启mysql
[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld&[root@ken home]# ss-tnl | grep 3306LISTEN0 70 :::3306 :::*
3.主节点上授权具有复制权限的用户
[root@ken home]# mysql -uroot -p <
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection idis 14Server version:5.7.23-log MySQL Community Server (GPL)
Copyright (c)2000, , Oracle and/or its affiliates. All rights reserved.
Oracleis 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> grant replication slave on *.* to ken@'%' identified by 'xx'; <
配置从节点
1.修改从节点和主机点时间同步
可以使用NTP服务或者date直接设置。此步省略。
2.修改丛节点配置文件
...
#binlog54 #binlog_format =STATEMENT55 binlog_format = row <
62 expire_logs_days = 10
63#procedure64 log_bin_trust_function_creators=1
65...
3.重启mysql
[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld&[root@ken home]# ss-tnl | grep 3306LISTEN0 70 :::3306 :::*
4.连接主服务器
链接主服务器命令详解
1 格式:CHANGE MASTER TO 选项2 选项:3 MASTER_HOST = 'host_name'指定主服务的ip或者主机名4 MASTER_USER = 'user_name' 指定主服务器的用户名
5 MASTER_PASSWORD = 'password' 指定用户名的密码
6 MASTER_PORT =port_num 指定连接的端口,默认是33067 MASTER_CONNECT_RETRY =interval 指定连接失败的时候的重试间隔时间
MySQL [(none)]> change master to master_host='10.220.5.137',master_user='root',master_password='xx'
5.启动 从节点
MySQL [(none)]> start salve;
6.查看从节点链接状态
主要看IO以及SQL线程是否启动
MySQL [(none)]>show slave status\G*************************** 1. row ***************************Slave_IO_State: Waitingfor master to send eventMaster_Host:10.220.5.137Master_User: root
Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos:928Relay_Log_File: relay-bin.000005Relay_Log_Pos:1141Relay_Master_Log_File: mysql-bin.000003Slave_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:0Last_Error:
Skip_Counter:0Exec_Master_Log_Pos:928Relay_Log_Space:1508Until_Condition: None
Until_Log_File:
Until_Log_Pos:0Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:0Last_IO_Error:
Last_SQL_Errno:0Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:1003306Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ec
Master_Info_File:/data/mysql/mysql3306/data/master.info
SQL_Delay:0SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waitingformore updates
Master_Retry_Count:86400Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 2a88f089-cd97-11e8-a862-000c29492f7b:1-45Auto_Position:0Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:1 row in set (0.00 sec)
7.测试
在主服务器端建库建表,查看从服务器端是否同步
主服务器建库建表
mysql>create database ken; <
Query OK,1 row affected (0.00sec)
mysql>use ken; <
Database changed
mysql> create table ken1 as select * fromjobs.teachers; <
Query OK,6 rows affected (0.04sec)
Records:6 Duplicates: 0 Warnings: 0mysql> select * fromken1; <
| TID | Name | Age | Gender |
+-----+------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | ken | 25 | M |
| 3 | ken | 45 | M |
| 4 | hah | 67 | M |
| 5 | ken2 | 76 | M |
| 6 | ken2 | 76 | M |
+-----+------------+-----+--------+
6 rows in set (0.00 sec)
8.检查从服务器
MySQL [(none)]>show databases; <
| Database |
+--------------------+
| information_schema |
| jobs |
| ken |
| liu |
| mysql |
| performance_schema |
| sys |
| test |
| test5 |
+--------------------+
9 rows in set (0.00sec)
MySQL [(none)]>use ken; <
Database changed
MySQL [ken]>show tables; <
| Tables_in_ken |
+---------------+
| ken1 |
+---------------+
1 row in set (0.00sec)
MySQL [ken]> select * fromken1; <
| TID | Name | Age | Gender |
+-----+------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | ken | 25 | M |
| 3 | ken | 45 | M |
| 4 | hah | 67 | M |
| 5 | ken2 | 76 | M |
| 6 | ken2 | 76 | M |
+-----+------------+-----+--------+
6 rows in set (0.00 sec)
到这里主从复制的演示就结束了,接下来再演示一个慢同步的架构
演示:实现mysql慢同步演示
半同步:N多个从节点中,只要任意一个从节点给主节点返回信息告知自己已经将数据存储成功,那么主节点会立刻给客户端反向执行结果信息。
配置主节点
1.安装主模块
mysql> install plugin rpl_semi_sync_master soname "semisync_master.so";
2.查看主端有关semi的变量
mysql> show global variables like '%semi%';+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.02 sec)
3.启动rpl_semi_sync_master_enabled
mysql> set global rpl_semi_sync_master_enabled=1;
4.安装从模块
mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so";
5.启动rpl_semi_sync_slave_enabled
mysql> set global rpl_semi_sync_slave_enabled=1;
6.重启从端slave
MySQL [ken]>stop slave;
MySQL [ken]> start slave;
7.查看IO/SQL启动状态
MySQL [ken]>show slave status\G*************************** 1. row ***************************Slave_IO_State: Waitingfor master to send eventMaster_Host:10.220.5.137Master_User: root
Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos:154Relay_Log_File: relay-bin.000003Relay_Log_Pos:367Relay_Master_Log_File: mysql-bin.000001Slave_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:0Last_Error:
Skip_Counter:0Exec_Master_Log_Pos:154Relay_Log_Space:568Until_Condition: None
Until_Log_File:
Until_Log_Pos:0Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:0Last_IO_Error:
Last_SQL_Errno:0Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:1003306Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ec
Master_Info_File:/data/mysql/mysql3306/data/master.info
SQL_Delay:0SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waitingformore updates
Master_Retry_Count:86400Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 2a88f089-cd97-11e8-a862-000c29492f7b:1-45Auto_Position:0Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:1 row in set (0.00 sec)
8.测试
略。详情见上主从复制。