直接使用tar包进行安装,
[root@server1 ~]# tar xf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
[root@server1 ~]# ls
mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-1.el7.x86_64.rpm
mysql-community-test-5.7.28-1.el7.x86_64.rpm
解压完成选择rpm包进行安装,两个节点都进行安装
[root@server1 ~]# yum install mysql-community-client-5.7.28-1.el7.x86_64.rpm mysql-community-common-5.7.28-1.el7.x86_64.rpm mysql-community-libs-5.7.28-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm mysql-community-server-5.7.28-1.el7.x86_64.rpm -y
安装完成配置主库
配置文件
[root@server1 ~]# vim /etc/my.cnf
log-bin=mysql-bin ##开启二进制日志
server-id=1 ##id号
启动服务
[root@server1 ~]# systemctl start mysqld
筛选数据库密码
[root@server1 ~]# cat /var/log/mysqld.log | grep pass
2020-04-15T09:28:56.805287Z 1 [Note] A temporary password is generated for root@localhost: SL2zBV?)jG(w
使用这个密码做初始化
[root@server1 ~]# mysql_secure_installation
做完初始化就可以登陆数据库进行操作了。
登陆进去进行授权
mysql> CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.example.com';
mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 693 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置从库
[root@server2 ~]# vim /etc/my.cnf
server-id=2
安全初始化
[root@server2 ~]# mysql_secure_installation
登陆数据库,指定master
mysql> change master to master_host='192.168.223.11' , ##指定主库地址
-> master_user='repl',master_password='password', ##认证密码
-> master_log_file='mysql-bin.000002', ##日志文件
-> master_log_pos=693; ##复制点
mysql> start slave; ##开启slave
mysql> show slave status\G##查看状态
Slave_IO_Running: Yes ##这两个没问题就完成了
Slave_SQL_Running: Yes
部署完成进行测试
server1上创建数据库,
mysql> create database hhaa;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hhaa |
| mysql |
| performance_schema |
| sys |
+--------------------+
server2上进行查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hhaa |
| mysql |
| performance_schema |
| sys |
+--------------------+
同步完成,下来测试数据
server1
mysql> create table aaa(
-> username varchar(10) not null,
-> password varchar(20) not null);
mysql> insert into aaa values('user1','123');
server2
mysql> select * from hhaa.aaa;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
基于gtid的主从复制
server1
配置文件
[root@server1 ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=1
gtid_mode=ON
enforce-gtid-consistency=true
进入数据库
mysql> use mysql;
mysql> select * from gtid_executed; ##内容为空
[root@server1 ~]# cd /var/lib/mysql
[root@server1 mysql]# mysqlbinlog mysql-bin.000002
如果是基于position的主从复制:将一个事件拆开来复制,如果一个事件进行的过程中出现问题,那么复制也会出现问题
如果是基于gtid的主从复制:一个以事件为单位进行复制,如果一个事件进行的过程中出现问题,那么复制也不会出现问题
[root@server1 mysql]# cat auto.cnf ##查看id
[auto]
server-uuid=87c64563-7efb-11ea-b696-000c2975af7b
[root@server1 mysql]# systemctl restart mysqld
server2
[root@server2 ~]# vim /etc/my.cnf
server-id=2
gtid_mode=ON
enforce-gtid-consistency=true ##开启gitd
[root@server2 ~]# systemctl restart mysqld
开启数据库进行配置
mysql> stop slave;
mysql> change master to master_host='192.168.223.11',master_user='repl',master_password='password',master_auto_position=1;
mysql> start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes ##保证主从复制是好的
Slave_SQL_Running: Yes
Retrieved_Gtid_Set: ##这两个是空的,等待数据写入
Executed_Gtid_Set:
server1写入数据
mysql> insert into aaa values ('user2','123');
mysql> insert into aaa values ('user3','123');
server2查看
mysql> show slave status\G;
Retrieved_Gtid_Set: 87c64563-7efb-11ea-b696-000c2975af7b:1-2
Executed_Gtid_Set: 87c64563-7efb-11ea-b696-000c2975af7b:1-2
并且可以查询到数据
mysql> use mysql
mysql> select * from gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 87c64563-7efb-11ea-b696-000c2975af7b | 1 | 1 |
| 87c64563-7efb-11ea-b696-000c2975af7b | 2 | 2 |
+--------------------------------------+----------------+--------------+