Ubuntu18.04_mysql5.7 主从备份配置

 

 

mysql5.7
服务器信息如下(实验环境,关闭机器的iptables防火墙和selinux):

node1 10.1.6.251物理数据库Master数据库
node210.1.6.252

物理数据库Slave数据库

osubuntu18.04 

 

在Master服务器上,增加一个用户账号(mysqlsync)作为同步用户账号.

mysql> GRANT REPLICATION SLAVE ON *.* to 'mysqlsync'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

刷新授权,不然授权在MySQL重启前不生效,执行这条指令后,即刻生效

flush privileges;

 

修改mysqld.cnf

libo@node1:/etc/mysql/mysql.conf.d$ sudo vim  /etc/mysql/mysql.conf.d/mysqld.cnf;

更改如下内容:

server-id               = 251
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db            = asd     #asd是需要同步的数据库名称

重启mysql服务:

libo@node1:/etc/mysql/mysql.conf.d$ systemctl restart mysql;
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: libo
Password: 
==== AUTHENTICATION COMPLETE ===
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
#把数据表先锁住,不让Position 变化!

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 | asd          |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

slave端:

libo@node2:/etc/mysql/mysql.conf.d$ sudo vim  /etc/mysql/mysql.conf.d/mysqld.cnf;

 

server-id               = 252
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db            = asd

 

 

slave端重启mysql

systemctl restart mysql

 在mysql控制台下输入匹配主从模式的命令

(此处使用的是root用户, 将当前数据库作为从库匹配到主数据库10.1.6.251的mysql服务上)

mysql> change master to \
    -> master_host="10.1.6.251", \
    -> master_user="root", \
    -> master_password="123456", \
    -> master_log_file="mysql-bin.000001", \
    -> master_log_pos=154;
ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.
mysql> show variables like '%repository%';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| master_info_repository    | FILE  |
| relay_log_info_repository | FILE  |
+---------------------------+-------+
2 rows in set (0.00 sec)

mysql> use mysql
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> drop table slave_master_info;
Query OK, 0 rows affected (0.04 sec)

mysql> drop table slave_relay_log_info;
Query OK, 0 rows affected (0.08 sec)

mysql> drop table slave_worker_info;
Query OK, 0 rows affected (0.03 sec)

mysql> drop table innodb_index_stats;
Query OK, 0 rows affected (0.06 sec)

mysql> drop table innodb_table_stats;
Query OK, 0 rows affected (0.05 sec)

slave端:

 重新启动mysql服务

root@node2:~# systemctl restart mysql

重新配置在mysql控制台下输入匹配主从模式的命令

mysql> change master to \
    -> master_host="10.1.6.251", \
    -> master_user="root", \
    -> master_password="123456", \
    -> master_log_file="mysql-bin.000001", \
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.27 sec)
mysql> show slave status\G

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值