1:mysql-主从复制
实验环境:server2为master server3为slave
1:安装mysql:
[root@server2 ~]# tar xf mysql-5.7.24-1.el6.x86_64.rpm-bundle.tar
[root@server2 ~]# yum install mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm -y
2:将安装包发送给server3
[root@server2 ~]# scp mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm server3:/root
3:编辑文件:
[root@server2 ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=2
4:开启mysql,登陆
[root@server1 ~]# systemctl start mysql
[root@server1 ~]# grep password /var/log/mysqld.log (查看密码,第一次初始化登陆是要用到)
[root@server1 ~]# mysql_secure_installation ##使用之前得到的密码,进行登陆,然后修改密码,
修改的密码,要有大小写字母,要有特殊字符,密码强度要达到一定的要求。第一个回车,后面的都y
[root@server1 ~]# mysql -p 登陆
mysql> show master status;
mysql> grant replication slave on *.* to repl@'172.25.60.%' identified by 'Yz123456@';#允许172.25.60.%网段使用repl用户复制自己
5:server3安装mysql,编辑配置文件(server3为从机)
[root@server3 ~]# yum install *
[root@server3 ~]# vim /etc/my.cnf
server-id=3
6:开启服务,获取密码
[root@server2 ~]# systemctl start mysqld
[root@server2 ~]# grep password /var/log/mysqld.log ##获取密码
[root@server2 ~]# mysql_secure_installation ##第一次初始化登陆,和server2上的操作一样
7:此时如果serevr3(slave)以master的身份进行登陆,发现是无法查看server2上的数据库的(记住一定要退出来,否则会影响后面的操作),因为还没有获得权限
[root@server3 ~]# mysql -h 172.25.60.2 -u repl -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
7:获取权限,登陆
[root@server2 ~]# mysql -pYz123456@
mysql>change master to master_host='172.25.60.1',master_user='repl',master_password='Yz123456@',master_log_file='mysql-bin.000002',master_log_pos=1003;(show master status;在主机中进行查看,千万不能错)
mysql> start slave;
mysql> show slave status\G; (这两个参数显示Yes说明主从复制成功)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
8:检测:
server2(master)上创建库westos
mysql> create database westos;
Query OK, 1 row affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
servre3上,也会显示westos(主从复制成功)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
2:Gitid方式的mysql主从复制:
Server2:master
Server3:slave
1:接着之前的实验环境,就不要重新在新的主机上安装myspl了,也可以使用新的主机,重新来做。
2:编辑配置文件:
[root@server2 ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2
gtid_mode=ON
enforce-gtid-consistency=true
重新开启myspld服务:
[root@server2 ~]# systemctl restart mysqld
[root@server3 ~]# vim /etc/my.cnf
[mysqld]
server-id=3
gtid_mode=ON
enforce-gtid-consistency=true
[root@server3 ~]# systemctl restart mysqld
3:登录(因为我是接着上面的主从复制的基础上做的,所以密码已经修改完了,如果是新的,就和上面的操作一样,修改密码)
[root@server2 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show master status; 查看file和pos值
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | |
| |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
4:设置权限(只允许172.25.60.3主机进行复制,以可以像上面的那个一样设置成一个网段的)
mysql> grant replication slave on *.* to yz@'172.25.60.3' identified by 'Yz123456@';
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5:slave访问控制
[root@server3 ~]# mysql -p ##登录
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>change master to MASTER_HOST='172.25.60.2',master_user='repl',master_password='Yz123456@',MASTER_AUTO_POSITION=1; ###基于grid的主从复制,就可以不知道master的file和post直接设置为自动的就好
ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
问题1:此时发现为什么无法成功呢?
发现与原来是之前的slave还开着,要先关闭
mysql> stop slave; ###关闭
Query OK, 0 rows affected (0.05 sec)
mysql> change master to MASTER_HOST='172.25.36.2',master_user='repl',master_password='Yz123456@',MASTER_AUTO_POSITION=1; ##成功
Query OK, 0 rows affected, 2 warnings (0.13 sec)
6:打开slave
mysql> start slave;必须先打开slave才能看到yes否则是no
mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
7:检测:server2(master)上创建库redhat
mysql> create database redhat;
Query OK, 1 row affected (0.06 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| redhat |
| sys |
| westos |
+--------------------+
6 rows in set (0.00 sec)
server3(slave)上:可以查看到redhat库,说明主从复制成功
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| redhat |
| sys |
| westos |
三:基于gtid的半同步复制
gitd主从复制:
在主上操作一个命令,不管从上io是否打开,西湖上立刻就执行,看不出来是否有错
基于gtid的半同步复制,如果从上的io关闭了,此时在主上进行一个操作,会有延迟,在真实的生产环境中,会将那个参数设置成无限大,此时会让主一直等待,此时我们就会知道出错了。
1:主从服务器安装半同步
Server2
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.08 sec)
Server3
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.45 sec)
mysql> SELECT PLUGIN_NAME,PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%'; ##查看状态
+----------------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+----------------------+---------------+
| rpl_semi_sync_master | ACTIVE |
+----------------------+---------------+
1 row in set (0.00 sec)
2:主从服务器打开半同步
server2
mysql> SET GLOBAL rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
server3
mysql> SET GLOBAL rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
3:在server2上创建westos库,usertb表,并向usertb表中写入数据,便于后面查看
mysql> use westos;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table usertb(
-> username varchar(10) not null,
-> password varchar(15) not null);
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+------------------+
| Tables_in_westos |
+------------------+
| usertb |
+------------------+
1 row in set (0.00 sec)
mysql> desc usertb;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(15) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> insert into usertb values ('user1','123');
Query OK, 1 row affected (0.00 sec)
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
1 row in set (0.00 sec)
mysql> insert into usertb values ('user3','123');
Query OK, 1 row affected (0.01 sec)
mysql> insert into usertb values ('user4','123');
Query OK, 1 row affected (0.01 sec)
mysql> insert into usertb values ('user5','123');
Query OK, 1 row affected (0.01 sec)
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user3 | 123 |
| user4 | 123 |
| user5 | 123 |
+----------+----------+
4 rows in set (0.00 sec)
4:此时在从机上查看:
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> select * from gtid_executed; ##可查看到
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 1 | 1 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 2 | 2 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 3 | 3 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 4 | 4 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 5 | 5 |
+--------------------------------------+----------------+--------------+
5 rows in set (0.00 sec)
注意:这里的1 2 3 4 5对应的不是我在主机上建立user1,user2 等用户,而对应的是在主机上进行的操作,可在主机的日志里进行查看。
[root@server2 mysql]# mysqlbinlog mysql-bin.000003 可在日志中查看到具体的1 2 3 4 5
5:查看io单口关闭时,主机上延迟的时间:
mysql> show status like '%rpl%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
mysql> show variables like '%rpl%'; ##延迟10000毫秒(当从机的io进程,此时主机上需要等待10秒)
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON |
| 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 |
| rpl_stop_slave_timeout | 31536000 |
+-------------------------------------------+------------+
6:关闭从机上的io端口
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)
7:检测(真机上进行操作,向之前的表中添加用户)
主上:需要等待10秒,
mysql> insert into usertb values ('user9','123');
Query OK, 1 row affected (0.01 sec)
从上:
mysql> select * from gtid_executed;发现没有加上去,之前也是8,没有加上去,等待10秒后,还是8个
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 1 | 1 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 2 | 2 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 3 | 3 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 4 | 4 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 5 | 5 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 6 | 6 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 7 | 7 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 8 | 8 |
8:当打开io端口之后,之前没有加上去的那个也会加上去
mysql> start slave io_thread;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from gtid_executed;此时io端口打开,之前没有的那个也加上去了,变成9了
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 1 | 1 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 2 | 2 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 3 | 3 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 4 | 4 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 5 | 5 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 6 | 6 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 7 | 7 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 8 | 8 |
| 45a921b3-37ce-11e9-b01f-52540045a6ea | 9 | 9 |
四:并行复制
并行复制(多主一从):复制到从数据库的表中,会发现修改完配置文件后,在没有重新启动服务之前
[root@server3 mysql]# cat relay-log.info 此时有这个文件,在重启服务之后,数据库的表中有,而这个文件没有了
7
./server3-relay-bin.000004
1545
mysql-bin.000003
3646
0
0
1
sql默认是单线程,不是只有一个线程,是只有一个主线程,设并行并行复制后变为多线程,加快日志的执行,减小延迟
1:主机上修改配置文件
[root@server2 ~]# vim /etc/my.cnf
slave-parallel-type=LOGICAL_CLOCK #基于组提交的并行复制
slave-parallel-workers=10 #开启10个worker,单线程转变为多线程
master_info_repository=TABLE #默认情况下是以文件形式保存,是刷磁盘,修改后用表保存,是保存在数据库中,所有的配置会记录在数据库中(优化选项)
relay_log_info_repository=TABLE
relay_log_recovery=ON #激活
2:重启mysqld服务
[root@server2 ~]# systemctl restart mysqld
3:从机上修改配置文件
slave-parallel-type=LOGICAL_CLOCK #基于组提交的并行复制
slave-parallel-workers=10 #开启16个worker,单线程转变为多线程
master_info_repository=TABLE #默认情况下是以文件形式保存,是刷磁盘,修改后用表保存,是调用数据库表(优化选项)
relay_log_info_repository=TABLE
relay_log_recovery=ON
4:重启服务
[root@server3 ~]# systemctl restart mysqld
5:查看master_info,查看线程的数量。
mysql> select * from slave_master_info; ###并行复制来的
+-----------------+------------------+----------------+-------------+-----------+---------------+------+---------------+-------------+--------+------------+----------+------------+---------+------------------------+-----------+------+--------------------+--------------------------------------+-------------+---------+-------------+-----------------------+--------------+-------------+
| Number_of_lines | Master_log_name | Master_log_pos | Host | User_name | User_password | Port | Connect_retry | Enabled_ssl | Ssl_ca | Ssl_capath | Ssl_cert | Ssl_cipher | Ssl_key | Ssl_verify_server_cert | Heartbeat | Bind | Ignored_server_ids | Uuid | Retry_count | Ssl_crl | Ssl_crlpath | Enabled_auto_position | Channel_name | Tls_version |
+-----------------+------------------+----------------+-------------+-----------+---------------+------+---------------+-------------+--------+------------+----------+------------+---------+------------------------+-----------+------+--------------------+--------------------------------------+-------------+---------+-------------+-----------------------+--------------+-------------+
| 25 | mysql-bin.000003 | 3646 | 172.25.36.2 | repl | Yz123456@ | 3306 | 60 | 0 | | | | | | 0 | 30 | | 0 | 45a921b3-37ce-11e9-b01f-52540045a6ea | 86400 | | | 1 | | |
+-----------------+------------------+----------------+-------------+-----------+---------------+------+---------------+-------------+--------+------------+----------+------------+---------+------------------------+-----------+------+--------------------+--------------------------------------+-------------+---------+-------------+-----------------------+--------------+-------------+
1 row in set (0.00 sec)
mysql> select * from slave_relay_log_info;
+-----------------+----------------------------+---------------+------------------+----------------+-----------+-------------------+----+--------------+
| Number_of_lines | Relay_log_name | Relay_log_pos | Master_log_name | Master_log_pos | Sql_delay | Number_of_workers | Id | Channel_name |
+-----------------+----------------------------+---------------+------------------+----------------+-----------+-------------------+----+--------------+
| 7 | ./server2-relay-bin.000019 | 4 | mysql-bin.000009 | 194 | 0 | 10 | 1 | |
+-----------------+----------------------------+---------------+------------------+----------------+-----------+-------------------+----+--------------+
mysql> select * from slave_worker_info ;
会有16个线程,等待调度器调度使用
mysql> show processlist; ##查看是否开启了之前设置的10个
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 184 | Waiting for master to send event | NULL |
| 2 | system user | | NULL | Connect | 184 | Slave has read all relay log; waiting for more updates | NULL |
| 4 | system user | | NULL | Connect | 875 | Waiting for an event from Coordinator | NULL |
| 5 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 6 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 7 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 8 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 9 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 10 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 11 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 12 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 13 | system user | | NULL | Connect | 184 | Waiting for an event from Coordinator | NULL |
| 15 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
13 rows in set (0.00 sec)
Binlog Dump GTID :监听master,当master有动作后,将日志发送给slave。可以通过查看日志查看具体信息。
五:全同步复制
全同步的每个服务器都是主服务器,也是从服务器。
注意:server2和server3恢复数据库状态,关闭数据库,删除数据库内容
server2上:
1:删除之前的数据库内容
[root@server2 mysql]# systemctl stop mysqld
[root@server2 mysql]# ls
auto.cnf ib_buffer_pool mysql-bin.000001 private_key.pem westos
ca-key.pem ibdata1 mysql-bin.000002 public_key.pem
ca.pem ib_logfile0 mysql-bin.000003 server-cert.pem
client-cert.pem ib_logfile1 mysql-bin.index server-key.pem
client-key.pem mysql performance_schema sys
[root@server2 mysql]# rm -rf *
[root@server2 mysql]# ls
[root@server2 mysql]# vim /etc/my.cnf
[root@server2 mysql]# systemctl start mysqld
2:编辑配置文件
[root@server2 mysql]# vim /etc/my.cnf
全同步:
server-id=2 #每个节点不同
gtid_mode=ON
enforce-gtid-consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="9d79d675-fad5-44dd-a9a2-2145d5698685" #只要是
UUid格式就可以,但三个节点要保持一致 ##uuipgen获取uuip
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.60.2:24901" #每个节点的ip
loose-group_replication_group_seeds= "172.25.60.2:24901,172.25.60.3:24901,172.25.60.4:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
loose-group_replication_ip_whitelist="172.25.60.0/24,127.0.0.1"
3:开启mysqld服务,获取密码,登录。
[root@server2 mysql]# grep password /var/log/mysqld.log
2019-02-24T08:10:32.288208Z 1 [Note] A temporary password is generated for root@localhost: gs2CuZkeNY*f
[root@server2 mysql]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24-log
Copyright (c) 2000, 2018, 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> ALTER USER root@localhost identified by 'Yz123456@';
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
4:根据官方文档进行操作:最后server2一定要显示online
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> create user rpl_user@'%' identified by "Yz123456@";
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_user='rpl_user',master_password='Yz123456@' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.05 sec)
mysql> set global group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> start group_replication;
Query OK, 0 rows affected (2.08 sec)
mysql> set global group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from performance_schema.replication_group_members; ##一点要显示online
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | a7d15aa3-380b-11e9-a6b0-52540045a6ea | server2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)
5:建立库,表,看是否能够正常使用
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table t1(cl int primary key,c2 text not null);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t1 values (1,'luis');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+------+
| cl | c2 |
+----+------+
| 1 | luis |
+----+------+
1 row in set (0.00 sec)
server3上:
1:删除之前的数据库文件,重启服务
[root@server3 mysql]# systemctl stop mysqld
[root@server3 mysql]# ls
auto.cnf ib_buffer_pool mysql-bin.000001 private_key.pem westos
ca-key.pem ibdata1 mysql-bin.000002 public_key.pem
ca.pem ib_logfile0 mysql-bin.000003 server-cert.pem
client-cert.pem ib_logfile1 mysql-bin.index server-key.pem
client-key.pem mysql performance_schema sys
[root@server3 mysql]# rm -rf *
[root@server3 mysql]# ls
[root@server3 mysql]# vim /etc/my.cnf
[root@server3 mysql]# systemctl start mysqld
2:编辑配置文件:
server-id=3 #每个节点不同
gtid_mode=ON
enforce-gtid-consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="9d79d675-fad5-44dd-a9a2-2145d5698685" #只要是
UUid格式就可以,但三个节点要保持一致 ##uuipgen获取uuip
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.60.3:24901" #每个节点的ip
loose-group_replication_group_seeds= "172.25.60.2:24901,172.25.60.3:24901,172.25.60.4:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
loose-group_replication_ip_whitelist="172.25.60.0/24,127.0.0.1"
3:查看数据库进程,确保没有数据库进程,获取密码,登录
[root@server3 ~]# ps ax | grep mysqld
2493 pts/0 S+ 0:00 grep --color=auto mysqld
[root@server3 ~]# systemctl start mysqld
[root@server3 ~]# grep password /var/log/mysqld.log
2019-02-24T08:54:53.942129Z 1 [Note] A temporary password is generated for root@localhost: iBzhOxfg5X,1
4:登录数据库,根据官方文档一比一步来,最后要显示online
[root@server3 mysql]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log
Copyright (c) 2000, 2018, 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> alter user root@localhost identified by "Yz123456@";
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user rpl_user@'%' identified by 'Yz123456@';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_user='rpl_user',master_password='Yz123456@' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.05 sec)
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)
mysql> start group_replication;
Query OK, 0 rows affected (5.82 sec)
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | a7d15aa3-380b-11e9-a6b0-52540045a6ea | server2 | 3306 | ONLINE |
| group_replication_applier | ab1aa0d8-380f-11e9-927c-525400db07bf | server3 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
server4:此处我的server4是全新的,如果之前也搭建过数据库,记得清楚之前的数据库文件
server4的操作和server3一样
1:编辑配置文件
server-id=3 #每个节点不同
gtid_mode=ON
enforce-gtid-consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="9d79d675-fad5-44dd-a9a2-2145d5698685" #只要是
UUid格式就可以,但三个节点要保持一致 ##uuipgen获取uuip
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.60.3:24901" #每个节点的ip
loose-group_replication_group_seeds= "172.25.60.2:24901,172.25.60.3:24901,172.25.60.4:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
loose-group_replication_ip_whitelist="172.25.60.0/24,127.0.0.1"
2:获取密码,登录数据库,最后显示servre2,server3,server4都是online的
[root@server4 ~]# systemctl start mysqld
[root@server4 ~]# grep password /var/log/mysqld.log
2019-02-24T08:54:53.942129Z 1 [Note] A temporary password is generated for root@localhost: iBzhOxfg5X,1
[root@server4 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log
Copyright (c) 2000, 2018, 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> alter user root@localhost identified by "Yz123456@";
Query OK, 0 rows affected (0.01 sec)
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user rpl_user@'%' identified by 'Yz123456@';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_user='rpl_user',master_password='Yz123456@' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.02 sec)
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)
mysql> start group_replication;
Query OK, 0 rows affected (3.30 sec)
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | a7d15aa3-380b-11e9-a6b0-52540045a6ea | server2 | 3306 | ONLINE |
| group_replication_applier | ab1aa0d8-380f-11e9-927c-525400db07bf | server3 | 3306 | ONLINE |
| group_replication_applier | da498c74-3811-11e9-8a5f-52540023ad98 | server4 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
检测:server4上创建用户,在server2 server3都能查看到。server2,server3,server4都是一样的。在任何一个上面进行操作,其他两处都可以查看到
mysql> use test;
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 t1;
+----+------+
| cl | c2 |
+----+------+
| 1 | luis |
+----+------+
1 row in set (0.00 sec)
mysql> insert into t1 values (2,'yz');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+------+
| cl | c2 |
+----+------+
| 1 | luis |
| 2 | yz |
+----+------+
2 rows in set (0.00 sec)
mysql>
六:读写分离
实验环境server4充分调度机
server2和server3实现主从复制 server2为master,srevre3为slave
真机充当客户端
1、what 什么是读写分离?
读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。
2、why 为什么要读写分离呢?
因为数据库的“写”(写10000条数据到oracle可能要3分钟)操作是比较耗时的。
但是数据库的“读”(从oracle读10000条数据可能只要5秒钟)。
所以读写分离,解决的是,数据库的写入,影响了查询的效率。
3、when 什么时候要读写分离?
数据库不一定要读写分离,如果程序使用数据库较多时,而更新少,查询多的情况下会考虑使用,利用数据库主从同步 。可以减少数据库压力,提高性能。当然,数据库也有其它优化方案。memcache 或是 表折分,或是搜索引擎。都是解决方法。
4、主从复制与读写分离
在实际的生产环境中,对数据库的读和写都在同一个数据库服务器中,是不能满足实际需求的。无论是在安全性、高可用性还是高并发等各个方面都是完全不能满足实际需求的。因此,通过主从复制的方式来同步数据,再通过读写分离来提升数据库的并发负载能力。有点类似于前面我们学习过的rsync,但是不同的是rsync是对磁盘文件做备份,而mysql主从复制是对数据库中的数据、语句做备份。
5、 mysql读写分离原理
读写分离就是在主服务器上修改,数据会同步到从服务器,从服务器只能提供读取数据,不能写入,实现备份的同时也实现了数据库性能的优化,以及提升了服务器安全。
[root@server4 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/ ##解压安装包
[root@server4 ~]# cd /usr/local/
[root@server4 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-proxy-0.8.5-linux-el6-x86-64bit share
[root@server4 local]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
[root@server4 local]# ls
bin etc games include lib lib64 libexec mysql-proxy sbin share src
[root@server4 local]# cd mysql-proxy/
[root@server4 mysql-proxy]# ls
bin include lib libexec licenses share
[root@server4 mysql-proxy]# cd bin/
[root@server4 bin]# pwd
/usr/local/mysql-proxy/bin
[root@server4 bin]# vim ~/.bash_profile ##配置环境变量
PATH=$PATH:$HOME/bin:/usr/local/mysql-proxy/bin
[root@server4 bin]# source ~/.bash_profile
[root@server4 bin]# cd ..
[root@server4 mysql-proxy]# mkdir conf
[root@server4 mysql-proxy]# mkdir log
[root@server4 mysql-proxy]# cd conf/
[root@server4 conf]# vim mysql-proxy.conf ##注意:配置文件中不能有注释,不能有空格否则会出错(一个一个进行检查,之前就是载这里出错的,一定要小心)
[mysql-proxy]
daemon=true #守护进程
user=root #执行mysqql-proxy的用户
pid-file=/usr/local/mysql-proxy/log/mysql-proxy.pid
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
log-level=info #日志级别
keepalive=true
proxy-address=172.25.36.4:3306 #调度器
proxy-read-only-backend-addresses=172.25.36.3:3306 #slave
proxy-backend-addresses=172.25.36.2:3306 #master
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
plugins=proxy
[root@server4 conf]# cd ..
[root@server4 mysql-proxy]# cd share/doc/mysql-proxy/
[root@server4 mysql-proxy]# pwd
/usr/local/mysql-proxy/share/doc/mysql-proxy
[root@server4 mysql-proxy]# vim rw-splitting.lua
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, #进程超过1个后,开始读写分离
max_idle_connections = 2,
is_debug = false
}
end
[root@server4 mysql-proxy]mysql-proxy
--defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf ##文件权限过大,导致出错,要修改权限
2019-02-24 20:11:17: (critical) mysql-proxy-cli.c:326: loading config from '/usr/local/mysql-proxy/conf/mysql-proxy.conf' failed: permissions of /usr/local/mysql-proxy/conf/mysql-proxy.conf aren't secure (0660 or stricter required)
2019-02-24 20:11:17: (message) Initiating shutdown, requested from mysql-proxy-cli.c:328
2019-02-24 20:11:17: (message) shutting down normally, exit code is: 1
[root@server4 mysql-proxy]# cd ..
[root@server4 doc]# cd ..
[root@server4 share]# ls
doc
[root@server4 share]# cd ..
[root@server4 mysql-proxy]# ls
bin conf include lib libexec licenses log share
[root@server4 mysql-proxy]# chmod 660 conf/mysql-proxy.conf ##修改权限
[root@server4 mysql-proxy]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@server4 mysql-proxy]# netstat -antlp ##查看3306端口,打开mysql-proxy 服务
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.25.36.4:3306 0.0.0.0:* LISTEN 2362/mysql-proxy
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 648/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 909/master
tcp 0 0 172.25.36.4:22 172.25.36.250:37906 ESTABLISHED 2145/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN 648/sshd
tcp6 0 0 ::1:25 :::* LISTEN 909/master
[root@server4 ~]# yum install lsof -y > /dev/null
[root@server4 mysql-proxy]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 2362 root 10u IPv4 25215 0t0 TCP server4:mysql (LISTEN)
在此之前serevr2和server3要实现主从复制。之前的实验可对接下来的实验造成影响
首先关闭mysql服务,server2和server3上删除/var/lib/mysql里面的数据,然后重新搭建组从复制
[root@server2 ~]# yum install lsof -y > /dev/null
[root@server2 ~]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2509 mysql 35u IPv6 25972 0t0 TCP *:mysql (LISTEN)
mysqld 2509 mysql 59u IPv6 26020 0t0 TCP server2:mysql->server3:42048 (ESTABLISHED)
[root@server2 ~]# mysql -pYz123456@
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: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database westos;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
mysql>
mysql> grant insert,update,select on westos.* to proxy@'172.25.36.%' identified by 'Yz123456@';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> create database westos;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
mysql>
mysql> grant insert,update,select on westos.* to proxy@'172.25.36.%' identified by 'Yz123456@';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> use westos;
Database changed
mysql> create table linux(
-> username varchar(10) not null,
-> username varchar(20) not null);
ERROR 1060 (42S21): Duplicate column name 'username'
mysql> create table linux( username varchar(10) not null, password varchar(20) not null);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into linux values ('yz','123')
-> ;
Query OK, 1 row affected (0.01 sec)
mysql> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| yz | 123 |
+----------+----------+
1 row in set (0.00 sec)
mysql> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| yz | 123 |
| yy | 666 |
+----------+----------+
2 rows in set (0.00 sec)
server3:
[root@server3 mysql]# yum install lsof -y > /dev/null
[root@server3 mysql]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2501 mysql 18u IPv6 25720 0t0 TCP *:mysql (LISTEN)
mysqld 2501 mysql 58u IPv4 25760 0t0 TCP server3:42048->server2:mysql (ESTABLISHED)
真机(客户端):
[root@foundation36 Desktop]# mysql -uproxy -p -h 172.25.36.4
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases; ##查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| westos |
+--------------------+
MySQL [(none)]> use westos;
Database changed
MySQL [westos]> show tables;
Empty set (0.00 sec)
MySQL [westos]>
MySQL [westos]> show tables;
+------------------+
| Tables_in_westos |
+------------------+
| linux |
+------------------+
1 row in set (0.01 sec)
MySQL [westos]> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| yz | 123 |
+----------+----------+
1 row in set (0.00 sec)
MySQL [westos]> insert into linux values ('yy','666'); ##建立用户(插入数据)
Query OK, 1 row affected (0.01 sec)
MySQL [westos]> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| yz | 123 |
| yy | 666 |
+----------+----------+
2 rows in set (0.00 sec)
server2(主)
#只要master上有数据,则一定是写在了master上
因为server2和serevr3实现主从复制,server3上也能查看到
mysql> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| yz | 123 |
| yy | 666 |
+----------+----------+
2 rows in set (0.00 sec)
之前设置了:
min_idle_connections = 1, #进程超过1个后,开始读写分离
max_idle_connections = 2,
此时在真机上开多个进程来模拟远程客户连接数据库:
在只连接一个进程时:此时可以进行建立库,表等写的操作:
此时在调度器上执行 losf -i :3306
可以看到此时调度器调度的是server2(master)
当真机上有多个进程远程连接数据库时,此时执行
此时再在server4(调度机)上执行 losf -i :3306
此时可以看到调度器调度的是server3(slave)
###############################################
* mysql的二进制日志
###############################################
1.二进制备份的三种选择
2.日志的截取
贴心参数
minimal:精简后的日志
##############################################################
che_size | 3276 :二进制日志所占用的内存大小
max_binlog_size :日志最大值,默认1G,当日志到达1G时,将日志进行截取
sync_binlog=0 :性能快,应用层只是将数据提交给数据库,不写入磁盘,当提交到达一定值后将数据一次性刷入磁盘。=1后,降低了性能,提高了数据的完整行,每一次提交都会刷入磁盘。
query_cache_type=1 :对于查询的缓存,配置后要重启数据库,可以提高性能,有一个变动,这张表全部清掉
expire_logs_days:日志过期世间,单位是天,0表示不过期
slow_query_log: 数据库慢查询,监控数据库中过慢的日志,可以设置阀值,超过阀值的数据被记录
mysql优化经验
mysqldumpslow :慢查询排序工具
##############################################################
CHANGE MASTER TO MASTER_DELAY = N;设置主从延迟时间,可以用于测试
###############################################################