前言
随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器。在此之前我们必须要保证每台MySQL服务器里的数据同步。数据同步我们可以通过MySQL内部配置就可以轻松完成,主要有主从复制和主主复制。
演示:实现mysql主从复制
配置主节点
1.修改主节点配置文件
...
#binlog
#binlog_format = STATEMENT
binlog_format = row <<主从节点保持一致,要么都是row,要么都是mixed
server-id = 1003306 <<主从节点的server_id必须保证不能一致
log-bin = /data/mysql/mysql3306/logs/mysql-bin <<定义二进制日志文件保存文件和格式
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days =
#procedure
log_bin_trust_function_creators= ...
2.重启mysql
[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld &
[root@ken home]# ss -tnl | grep
LISTEN ::: :::*
3.主节点上授权具有复制权限的用户
[root@ken home]# mysql -uroot -p <<登录mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. 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> grant replication slave on *.* to ken@'%' identified by 'xx'; <<创建ken用户,并设置密码xx
配置从节点
1.修改从节点和主机点时间同步
可以使用NTP服务或者date直接设置。此步省略。
2.修改丛节点配置文件
...
#binlog
#binlog_format = STATEMENT
binlog_format = row <<主从节点保持一致,要么都是row,要么都是mixed
server-id = <<主从节点的server_id必须保证不能一致
log-bin = /data/mysql/mysql3306/logs/mysql-bin <<定义二进制日志文件保存文件和格式
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days =
#procedure
log_bin_trust_function_creators= ...
3.重启mysql
[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld &
[root@ken home]# ss -tnl | grep
LISTEN ::: :::*
4.连接主服务器
链接主服务器命令详解
格式:CHANGE MASTER TO 选项
选项:
MASTER_HOST = 'host_name' 指定主服务的ip或者主机名
MASTER_USER = 'user_name' 指定主服务器的用户名
MASTER_PASSWORD = 'password' 指定用户名的密码
MASTER_PORT = port_num 指定连接的端口,默认是3306
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
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.137
Master_User: root
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ec
Master_Info_File: /data/mysql/mysql3306/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_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:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec)
7.测试
在主服务器端建库建表,查看从服务器端是否同步
主服务器建库建表
mysql> create database ken; <<创建数据库ken
Query OK, row affected (0.00 sec) mysql> use ken; <<进入数据库
Database changed
mysql> create table ken1 as select * from jobs.teachers; <<创建ken1表
Query OK, rows affected (0.04 sec)
Records: Duplicates: Warnings: mysql> select * from ken1; <<查看ken1表数据
+-----+------------+-----+--------+
| TID | Name | Age | Gender |
+-----+------------+-----+--------+
| | Song Jiang | | M |
| | ken | | M |
| | ken | | M |
| | hah | | M |
| | ken2 | | M |
| | ken2 | | M |
+-----+------------+-----+--------+
rows in set (0.00 sec)
8.检查从服务器
MySQL [(none)]> show databases; <<查看数据库,已经同步过来ken
+--------------------+
| Database |
+--------------------+
| information_schema |
| jobs |
| ken |
| liu |
| mysql |
| performance_schema |
| sys |
| test |
| test5 |
+--------------------+
rows in set (0.00 sec) MySQL [(none)]> use ken; <<进入到ken数据库中
Database changed
MySQL [ken]> show tables; <<查看库中的表,可以看到ken1表
+---------------+
| Tables_in_ken |
+---------------+
| ken1 |
+---------------+
row in set (0.00 sec) MySQL [ken]> select * from ken1; <<检查ken1表中的数据完整
+-----+------------+-----+--------+
| TID | Name | Age | Gender |
+-----+------------+-----+--------+
| | Song Jiang | | M |
| | ken | | M |
| | ken | | M |
| | hah | | M |
| | ken2 | | M |
| | ken2 | | M |
+-----+------------+-----+--------+
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 | |
| rpl_semi_sync_master_trace_level | |
| rpl_semi_sync_master_wait_for_slave_count | |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
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
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.137
Master_User: root
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ec
Master_Info_File: /data/mysql/mysql3306/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_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:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec)
8.测试
略。详情见上主从复制。