一,传统主从复制:
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。
MySQL之间数据复制的基础是二进制日志文件(binary log file)。一台MySQL数据库一旦启用二进制日志后,其作为master,它的数据库中所有操作都会以“事件”的方式记录在二进制日志中,其他数据库 作为slave通过一个I/O线程与主服务器保持通信,并监控master的二进制日志文件的变化,如果发现master二进制日志文件发生变化,则会把 变化复制到自己的中继日志中,然后slave的一个SQL线程会把相关的“事件”执行到自己的数据库中,以此实现从数据库和主数据库的一致性,也就实现了 主从复制。
二,基于gtid的主从复制
1、全局事物标识:global transaction identifieds。
2、GTID事物是全局唯一性的,且一个事务对应一个GTID。
3、一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
4、GTID用来代替classic的复制方法,不在使用binlog+pos开启复制。而是使用master_auto_postion=1的方式自动匹配GTID断点进行复制。
5、MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善。
6、在传统的slave端,binlog是不用开启的,但是在GTID中,slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。
GTID比传统复制的优势:
1、更简单的实现failover,不用以前那样在需要找log_file和log_Pos。
2、更简单的搭建主从复制。
3、比传统复制更加安全。
4、GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过。
mysql主从复制
主:server1 172.25.28.1
从:server2 172.25.28.2
安装
mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm
server1:
1,修改配置文件/etc/my.cnf
添加
server-id=1 这个和从机id不一样
log-bin=mysql-bin 开启日志,mysql-bin是日志名
#这里可以根据需要添加更多
2,启动mysql
/etc/init.d/mysqld start
3,查看密码
[root@server1 ~]# cat /var/log/mysqld.log | grep password
2018-08-08T08:40:53.361416Z 1 [Note] A temporary password is generated for root@localhost: ARlR8QJ7TC<r
4,安全初始化
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
server2:
1,修改配置文件/etc/my.cnf
添加
server-id=2
2,启动mysql
/etc/init.d/mysqld start
3,查看密码
[root@server2 ~]# cat /var/log/mysqld.log | grep password
2018-08-08T08:47:53.657748Z 1 [Note] A temporary password is generated for root@localhost: 5-G,cB:7c//T
4,安全初始化同 server1
mysql_secure_installation
主从复制
1,server1创建用户授权
mysql> grant replication slave on *.* to rep@'172.25.28.%' identified by 'Zming=1998'; replication表示复制权限
Query OK, 0 rows affected, 1 warning (0.12 sec)
此时可以在server2上尝试远程登录
[root@server2 ~]# mysql -u rep -p -h 172.25.28.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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)
2,server1查看master状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 842 | | | |
+------------------+----------+--------------+------------------+-------------------+
3,server2上设置server1为master
mysql> change master to master_host='172.25.28.1',master_user='rep',master_password='Zming=1998',master_log_file='mysql-bin.000003',master_log_pos=842;
Query OK, 0 rows affected, 2 warnings (0.68 sec)
4,server2开启slave
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
查看slave状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.28.1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 842
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
注意:不可以在从机上面修改主机信息,否则同步会出错
主从复制的测试
server1创建
mysql> create database test;
Query OK, 1 row affected (0.04 sec)
mysql> use test;
Database changed
mysql> create table userlist(
-> username varchar(15) not null,
-> password varchar(25) not null);
Query OK, 0 rows affected (0.57 sec)
mysql> desc userlist;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO | | NULL | |
| password | varchar(25) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> insert into userlist value('user1','123');
Query OK, 1 row affected (0.13 sec)
mysql> insert into userlist value('user2','456');
Query OK, 1 row affected (0.14 sec)
mysql> insert into userlist value('user3','789');
Query OK, 1 row affected (0.07 sec)zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
server2查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
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 userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 456 |
| user3 | 789 |
+----------+----------+
3 rows in set (0.00 sec)
成功!!!
基于GTID的主从复制
1,server1加入/etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=true
2,重启
[root@server1 ~]# /etc/init.d/mysqld restart
server2同上配置
在server2上配置server1为master
mysql> stop slave; 先停用slave
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='172.25.28.1',master_user='rep',master_password='Zming=1998',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.25 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.28.1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试:server1:删除数据
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| userlist |
+----------------+
1 row in set (0.00 sec)
mysql> delete from userlist where username='user3';
Query OK, 1 row affected (0.10 sec)
server2:查看数据
mysql> use test;
Database changed
mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 456 |
+----------+----------+
2 rows in set (0.00 sec)
成功!!!