mysql_主從同步

在這裏我就不說怎麼搭建 Mysql 數據庫了!若是有須要能夠參照我前面的博文。mysql

此博文主要說配置 Linux  數據庫   主從   下面咱們開始進入正題。web

master:192.168.31.200sql

slave:192.168.31.250數據庫

主:192.168.31.200   配置以下:ide

1.修改   /etc/my.cnf    添加如下配置信息優化

[mysqld]日誌

log-bin = mysql-bin      ##開啓 bin  日誌
server-id =1         ##指定
innodb-file-per-table =ON   ##開啓獨立表空間
skip_name_resolve=ON     ##禁止域名解析(優化的一部分_根據需求)server

binlog_do_db=web      ##須要同步的數據庫
binlog_do_db=mysql
binlog_do_db=webonline
binlog_do_db=hrip

保存   wq   input

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

相關文章
相關標籤/搜索