1、服务环境
master:192.168.2.150,centos7.6,mysql5.7
slave:192.168.2.151,centos7.6,mysql5.7
注意:两个库的msyql版本要保持一致
2、mysql安装步骤(192.168.2.150和192.168.2.151服务器可按照相同步骤安装)
a.下载MySQL 安装包
[root@localhost local]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
b.安装mysql 安装源
[root@localhost local]# yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
c.在线安装MySQL,该步骤时间比较长
[root@localhost local]# yum -y install mysql-community-server
d.以上可能会报错,如果报以下错:
===========================================================================================================================================================================================
Package 架构 版本 源 大小
===========================================================================================================================================================================================
正在安装:
mysql-community-libs x86_64 5.7.37-1.el7 mysql57-community 2.4 M
替换 mariadb-libs.x86_64 1:5.5.68-1.el7
mysql-community-libs-compat x86_64 5.7.37-1.el7 mysql57-community 1.2 M
替换 mariadb-libs.x86_64 1:5.5.68-1.el7
mysql-community-server x86_64 5.7.37-1.el7 mysql57-community 174 M
为依赖而安装:
mysql-community-client x86_64 5.7.37-1.el7 mysql57-community 25 M
mysql-community-common x86_64 5.7.37-1.el7 mysql57-community 311 k
事务概要
===========================================================================================================================================================================================
安装 3 软件包 (+2 依赖软件包)
总计:203 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
源 "MySQL 5.7 Community Server" 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确。
失败的软件包是:mysql-community-libs-compat-5.7.37-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
编辑yum源文件
[root@localhost local]# vim /etc/yum.repos.d/mysql-community.repo
设置gpgcheck=0,跳过秘钥检查,然后重复执行步骤c
e.启动mysql,并设置开机启动
[root@localhost local]# systemctl start mysqld
[root@localhost local]# systemctl enable mysqld
[root@localhost local]# systemctl daemon-reload
f.修改root登录密码
mysql安装完成后,生成的root的临时初始密码放在/var/log/mysqld.log文件中,拷贝出临时密码
[root@localhost local]# vim /var/log/mysqld.log
g.修改root的临时密码,并设置远程访问权限
[root@localhost local]# mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql!@#7896jmg';
Query OK, 0 rows affected (0.00 sec)
# 设置远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysql!@#7896jmg' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
h.开放防火墙3306端口,或者关闭防火墙,具体操作省略。
以上步骤就完成的了centos环境下的mysql安装。
3、主从搭建
a.工作原理
Master 数据库发生变化时,会立马记录到Binary log 日志文件中
Slave数据库启动一个I/O 进程,连接Master数据库,并请求Master变化的二进制日志
Slave I/O获取到二进制日志,并保存到slave的Relay log日志文件中
Slave的SQL进程定时检查Realy log是否变化,发生变化时就更新数据
b.修改mysql的配置文件
[root@localhost local]# vim /etc/my.cnf
master配置文件添加以下设置(192.168.2.150)
# 节点ID,确保唯一
server-id = 1
#开启mysql的binlog日志功能
log-bin = mysql-bin
#控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
sync_binlog = 1
#binlog日志格式,mysql默认采用statement,建议使用mixed
binlog_format = mixed
#binlog过期清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大
max_binlog_cache_size= 512m
#不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
binlog-ignore-db=mysql
# 自增值的偏移量
auto-increment-offset = 1
# 自增值的自增量
auto-increment-increment = 1
#跳过从库错误
slave-skip-errors = all
slave配置文件添加以下设置(192.168.2.151)
server-id = 2
log-bin=mysql-bin
relay-log = mysql-relay-bin
#忽略规则
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=information_schema.%
重启两个mysql,配置生效
c.master数据库,创建复制用户并授权(测试环境可以也直接使用root账号)
CREATE USER master_user IDENTIFIED BY '123456';
grant replication slave on *.* to 'master_user'@'192.168.2.150' identified by '123456';
FLUSH PRIVILEGES;
d.查看master的状态
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 154| | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
c.配置从库
mysql> CHANGE MASTER TO
MASTER_HOST = '192.168.2.150',
MASTER_USER = 'master_user',
MASTER_PASSWORD = '123456',
MASTER_PORT = 3306,
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=154,
MASTER_RETRY_COUNT = 60,
MASTER_HEARTBEAT_PERIOD = 10000;
# MASTER_LOG_FILE='mysql-bin.000005',#与主库File 保持一致
# MASTER_LOG_POS=120 , #与主库Position 保持一致
d.启动从库slave进程(巨坑:有些文档使用slave start,在mysql>5.7时使用start slave)
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
e.测试主重复制,在slave(192.168.2.151)中,登录数据库,然后执行以下脚本
mysql> SLAVE START; #开启复制
mysql> SHOW SLAVE STATUS\G #查看主从复制是否配置成功
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.150
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 828
Relay_Log_File: mysql-relay-bin.000088
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000006
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: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 828
Relay_Log_Space: 693
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: 1
Master_UUID: b6483844-9e89-11ec-8659-000c29e4cc0f
Master_Info_File: /var/lib/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: 60
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)
查看:Slave_IO_Running: YES、Slave_SQL_Running: YES,说明配置成功。
f.测试
在master(192.168.2.150)中创建数据库test,然后在slave(192.168.2.151)中查看test数据库是否同步过来。