Mysql主从部署
Mysql主从部署
基于Mysql 5.7.26-1
一、 安装Mysql
离线部署推荐使用 "RPM Bundle"
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
在线安装可使用yum源
https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
在线安装
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yum install -y mysql-5.7.26
离线安装
wget -O mysql-5.7.29-1.el7.x86_64.rpm-bundle.tar https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
rpm -ivh *.rpm
二、 修改配置文件
进入master节点, vi /etc/my.cnf
#set utf8mb4character-set-server=utf8mb4
#set default time zone
default-time-zone="+8:00"
#set mysql-master
#主数据库端ID号
server_id=1
#开启二进制日志
log-bin=mysql-bin
#将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件中
log-slave-updates
# 日志格式,使用混合格式(SBR与RBR混合,MySQL自动选择)
binlog-format=MIXED
#控制binlog的写入频率。每执行多少次事务写入一次(这个参数性能消耗很大,但可减小MySQL崩溃造成的损失)
sync_binlog=5
#二进制日志自动删除的天数,默认值为0,表示“没有自动删除”,启动时和二进制日志循环时可能删除
expire_logs_days=60
#将函数复制到slave
log_bin_trust_function_creators=1
###
gtid_mode=ON
enforce-gtid-consistency=true
进入slave节点, vi /etc/my.cnf
#set utf8mb4character-set-server=utf8mb4
#set default time zone
default-time-zone="+8:00"
#set mysql slave
server_id=2
log-bin=mysql-bin
binlog-format=MIXED
log-slave-updates
innodb_flush_log_at_trx_commit=0
#MySQL主从复制的时候,当Master和Slave之间的网络中断,但是Master和Slave无法察觉的情况下(比如防火墙或者路由问题)。Slave会等待slave_net_timeout设置的秒数后,才能认为网络出现故障,然后才会重连并且追赶这段时间主库的数据
slave-net-timeout=60
log_bin_trust_function_creators=1
# 从库只读,root用户除外
read_only=1
###
gtid_mode=ON
enforce-gtid-consistency=true
重启mysql
systemctl restart mysqld
三、 创建同步用户
在mysql 5.7 中,本地用户无法直接登陆,需要从启动日志中获取基础密码。
进入主库,创建同步用户
#获取登陆密码grep password /var/log/mysqld.log
>2019-07-01T11:02:46.684952Z 1 [Note] A temporary password is generated for root@localhost: asdfg%hPWn
#登陆并创建slave用户
mysql -u root -p
>asdfg%hPWn
>CREATE USER `slave`@`192.168.%` IDENTIFIED BY "password";
>GRANT Lock Tables, Replication Client, Replication Slave ON *.* TO `slave`@`192.168.%`;
>flush privileges;
四、 登陆创建同步任务
登陆主库,获取binlog文件名.
ssh masetermysql -u root -p
> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
可以看出binlog文件名当前为 mysql-bin.000001
登陆从库,设置并开始同步。
ssh slavemysql -u root -p
>change master to master_host="192.168.xx.xxx",master_user="slave",master_password="password",master_auto_position=1;
>start slave;
>show slave statusG;
#当看到如下两项为yes时,设置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#同步状态为如下字段
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
validate_password_poicy=0
grep "root@local" /var/log/mysqld.log
mysqladmin -uroot -p${old_password} password Yunxi711
mysql -uroot -pYunxi711
CREATE USER "admin"@"%" IDENTIFIED BY "Yunxi711";
GRANT ALL ON *.* TO "admin"@"%";
flush privileges;
以上是 Mysql主从部署 的全部内容, 来源链接: utcz.com/z/533217.html