欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql_主从同步

程序员文章站 2022-03-09 14:33:01
在这里我就不说怎么搭建 Mysql 数据库了!如果有需要可以参照我前面的博文。 此博文主要说配置 Linux 数据库 主从 下面我们开始进入正题。 master:192.168.31.200 slave:192.168.31.250 主:192.168.31.200 配置如下: 1.修改 /etc/ ......

在这里我就不说怎么搭建 mysql 数据库了!如果有需要可以参照我前面的博文。

此博文主要说配置 linux  数据库   主从   下面我们开始进入正题。

master:192.168.31.200

slave:192.168.31.250

主:192.168.31.200   配置如下:

1.修改   /etc/my.cnf    添加以下配置信息

[mysqld]

log-bin = mysql-bin      ##开启 bin  日志
server-id =1         ##指定
innodb-file-per-table =on   ##开启独立表空间
skip_name_resolve=on     ##禁止域名解析(优化的一部分_根据需求)

binlog_do_db=web      ##需要同步的数据库
binlog_do_db=mysql
binlog_do_db=webonline
binlog_do_db=hr

保存   wq   

2.重启数据库  

/etc/init.d/mysqld retart

 

3.进入数据库

[root@mysqlmaster ~]# mysql -uroot -p123456
warning: using a password on the command line interface can be insecure.
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 4
server version: 5.6.42-log mysql community server (gpl)

copyright (c) 2000, 2018, oracle and/or its affiliates. all rights reserved.

oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql>

4.查看 master 值

mysql> show master status;
+------------------+----------+------------------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+------------------------+------------------+-------------------+
| mysql-bin.000002 | 120 | web,mysql,webonline,hr | | |
+------------------+----------+------------------------+------------------+-------------------+
1 row in set (0.01 sec)

 

5.创建一个用户给主从链接

mysql> grant replication slave on *.* to 'lihong'@'192.168.31.250' identified by '123456';

 

从:192.168.31.250   配置如下:

1.修改   /etc/my.cnf    添加以下配置信息

[mysqld]

server-id=2
relay-log=relay-log
relay-log-index=relay-log.index
log-bin = mysql-bin
innodb-file-per-table =on
skip_name_resolve=on

binlog_do_db=web
binlog_do_db=mysql
binlog_do_db=webonline
binlog_do_db=hr

保存   wq   

 

2.重启数据库  

/etc/init.d/mysqld retart

 

3.进入数据库

[root@mysqlmaster ~]# mysql -uroot -p123456
warning: using a password on the command line interface can be insecure.
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 4
server version: 5.6.42-log mysql community server (gpl)

copyright (c) 2000, 2018, oracle and/or its affiliates. all rights reserved.

oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql>

 

4.关闭 slave 

mysql> stop slave;
query ok, 0 rows affected, 1 warning (0.00 sec)

 

5.创建一个用户给主从链接

mysql> grant replication slave on *.* to 'lihong'@'192.168.31.250' identified by '123456';

 

6.配置关联的信息

mysql> change master to
-> master_host='192.168.31.200',
-> master_user='lihong',
-> master_password='123456',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=120;

 

7.开启 slave 

mysql> start slave;
query ok, 0 rows affected (0.03 sec)

 

8.查看 slave 是否正常开启了以及是否同步了

mysql> show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.31.200
master_user: lihong
master_port: 3306
connect_retry: 60
master_log_file: mysql-bin.000002
read_master_log_pos: 120
relay_log_file: relay-log.000003
relay_log_pos: 283
relay_master_log_file: mysql-bin.000002
slave_io_running: yes
slave_sql_running: yes
replicate_do_db:

 

9.是否同步成功,查看这两个状态值

slave_io_running: yes
slave_sql_running: yes