CentOS7下RPM模式搭建MySQL5.7.28主从复制

好久不见. 提交于 2020-03-16 00:32:54

CentOS7下RPM模式搭建MySQL5.7.28主从复制

一、配置文件:

主库(192.168.128.111):

[client]
port=3306
socket=/approot/data/mysql/mysql.sock
default-character-set=utf8

[mysql]
port=3306
# 设置mysql客户端默认字符集
default-character-set=utf8

[mysqld]
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id=111
# 只能用IP地址检查客户端的登录,不用主机名
# skip_name_resolve=0
# 设置3306端口
port=3306
# 设置mysql数据库的数据的存放目录
#datadir=/var/lib/mysql
#datadir=/approot/data/mysql/data
datadir=/approot/data/mysql
log-error=/var/log/mysqld.log
#socket=/var/lib/mysql/mysql.sock
socket=/approot/data/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid

# 支持符号链接
symbolic-links=0
# 允许最大连接数
max_connections=500
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
character-set-server=utf8
collation-server=utf8_general_ci
# SQL模式
sql_mode=STRICT_TRANS_TABLES,ANSI_QUOTES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO
# 设置时间戳的默认属性
explicit_defaults_for_timestamp=true
# 是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names=1
# 每个innodb表数据单独文件保存
innodb_file_per_table=ON
# 增加每个进程的可打开文件数量
open-files-limit=6000

######################### 主从复制设置 #############################
# 开启mysql binlog功能
log-bin=mysql-bin.log
# binlog记录内容的方式,记录被操作的每一行
binlog_format=MIXED
# 减少记录日志的内容,只记录受影响的列
binlog_row_image=minimal
# 指定需要复制的数据库名为testdb
binlog-do-db=testdb
# binlog过期清理时间
expire_logs_days=7
# 每个binlog日志文件大小
max_binlog_size=100M
# binlog缓存大小 
binlog_cache_size=4M
# binlog最大缓存大小
max_binlog_cache_size=100M
# 不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
binlog-ignore-db=mysql
# 自增值的偏移量
auto-increment-offset=1
# 自增值的自增量
auto-increment-increment=1
# 跳过从库错误,下面的配置是忽略1062的错误
# slave-skip-errors=1062

从库(192.168.128.112)

[client]
port=3306
socket=/approot/data/mysql/mysql.sock
default-character-set=utf8

[mysql]
port=3306
# 设置mysql客户端默认字符集
default-character-set=utf8

[mysqld]
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id=112
# 只能用IP地址检查客户端的登录,不用主机名
# skip_name_resolve=0
# 设置3306端口
port=3306
# 设置mysql数据库的数据的存放目录
#datadir=/var/lib/mysql
#datadir=/approot/data/mysql/data
datadir=/approot/data/mysql
log-error=/var/log/mysqld.log
#socket=/var/lib/mysql/mysql.sock
socket=/approot/data/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid

# 支持符号链接
symbolic-links=0
# 允许最大连接数
max_connections=500
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
character-set-server=utf8
collation-server=utf8_general_ci
# SQL模式
sql_mode=STRICT_TRANS_TABLES,ANSI_QUOTES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO
# 设置时间戳的默认属性
explicit_defaults_for_timestamp=true
# 是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names=1
# 每个innodb表数据单独文件保存
innodb_file_per_table=ON
# 增加每个进程的可打开文件数量
open-files-limit=6000
# 限制从库只读,但是此限制对拥有SUPER权限的用户均无效。
read_only=ON

######################### 从库配置 #############################
# relay_log的位置和名称,如果值为空,则默认位置在数据文件的目录(datadir) 
relay-log=relay-log
# 定义relay_log的位置和名称
relay-log-index=relay-log.index
# 是否自动清空不再需要中继日志时。默认值为1(启用)
relay_log_purge=1
# 当slave从库宕机后,假如relay-log损坏了,则自动放弃所有未执行的relay-log,并且重新从master上获取日志
relay_log_recovery=1
# 当设置为1时,slave的I/O线程每次接收到master发送过来的binlog日志都要写入系统缓冲区,然后刷入relay_log中继日志里
sync_relay_log=0
# 与sync_relay_log的配置含义相似 
sync_relay_log_info=0

二、安装前准备工作,两台主机都要做

1、配置网络、防火墙、内核安全

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
#BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
IPV6INIT=no
IPV4_FAILURE_FATAL=no
NAME=ens33
UUID=a9ef0538-d1ea-4f84-8ce6-9446304c9ed4
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.128.112
GATEWAY=192.168.128.2
IPV6_PRIVACY=no
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8
DNS3=8.8.4.4
systemctl restart network
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld

vim /etc/selinux/config
设置:SELINUX=disabled
setenforce 0

三、安装并配置mysql,但一定不要启动,两台安装方法相同

准备安装介质:
mkdir -p /approot/software/
scp -r root@192.168.128.111:/approot/software/mysql/ /approot/software/
或者直接下载后解压也可以
tar -xf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar  -C /approot/software/mysql

预先检查卸载mariadb
rpm -qa | grep mariadb
rpm -ev --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
预先检查卸载mysql
rpm -qa|grep -i mysql
rpm -ev --nodeps mysql-community-server-5.7.28-1.el7.x86_64
rpm -ev --nodeps mysql-community-client-5.7.28-1.el7.x86_64
rpm -ev --nodeps mysql-community-common-5.7.28-1.el7.x86_64
rpm -ev --nodeps mysql-community-libs-5.7.28-1.el7.x86_64

安装mysql
cd /approot/software/mysql
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
yum install libaio
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm

四、分别配置并启动两台mysql,因为主从都修改默认的datadir,因此需要在启动前进行配置。

1、分别将主从的配置复制到各自主机的/etc/my.cnf中。

2、分别在两台主机上创建文件夹并授权

mkdir -p /approot/data/mysql
chown -R mysql:mysql /approot/data/mysql
ll -d /approot/data/mysql

另开一个监视日志的终端,方便查看动态初始化密码
tail -F /var/log/mysqld.log
如果不开,也可以启动后打开查找,或命令查找
grep "password" /var/log/mysqld.log

比如查看到结果为:
[Note] A temporary password is generated for root@localhost: bBq*oaqc0ryE

3、启动mysql服务

systemctl start mysqld
根据上面方法获取到密码登陆mysql:
[root@hadoop100 etc]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28

4、修改root密码

set global validate_password_policy=LOW;
set global validate_password_length=5;
set password for root@localhost=password('leizm');
grant all privileges on *.* to 'root'@'%' identified by 'leizm';
flush privileges;

5、如果是主库,则在主库上创建个同步数据的账户,用来主从复制,提供给从库访问用。

create user 'repl'@'192.168.128.%' identified by 'leizm';
grant replication slave on *.* to 'repl'@'192.168.128.%';
flush privileges;

两台mysql都启动后就剩下同步了

五、主从同步启动复制,先操作主库、再操作从库

1、主库上获取同步点

mysql> flush tables with read lock;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      715 | testdb       | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> unlock tables;

2、从库根据主库同步点执行同步

stop slave;
change master to
 master_host='192.168.128.111',
 master_port=3306,
 master_user='repl',
 master_password='leizm',
 master_log_file='mysql-bin.000004',
 master_log_pos=715;
start slave;
show slave status\G;


上面命令执行结果:
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    ->  master_host='192.168.128.111',
    ->  master_port=3306,
    ->  master_user='repl',
    ->  master_password='leizm',
    ->  master_log_file='mysql-bin.000004',
    ->  master_log_pos=715;
start slave;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.128.111
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 715
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             Slave_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: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 715
              Relay_Log_Space: 521
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 111
                  Master_UUID: af18a083-66c1-11ea-98c7-0050563d66c2
             Master_Info_File: /approot/data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

同步成功!

有更多的配置信息和常见问题,可参考:

https://blog.csdn.net/sky__liang/article/details/85684615

https://blog.csdn.net/daicooper/article/details/79905660

https://www.cnblogs.com/cjsblog/archive/2018/09/26/9706370.html

https://blog.csdn.net/qq_36441027/article/details/81139209

https://zixuephp.net/article-440.html

https://blog.csdn.net/juded/article/details/54600294

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!