主庫服務器環境:
操作系統:Centos6.7
數據庫:Mysql5.6.34
IP: 192.168.10.1
從庫服務器環境:
操作系統:Centos6.7
數據庫:Mysql5.6.34
IP:192.168.10.2
查看centos版本
[sparkadmin@hadoop4 ~]$ cat /etc/redhat-release
CentOS release 6.7 (Final)
查看mysql版本
[sparkadmin@hadoop4 ~]$ mysql -V
mysql Ver 14.14 Distrib 5.6.34, for Linux (x86_64) using EditLine wrapper
首先在機器分別安裝mysql
配置:
1、主服務器
1.1、創建一個複製用戶,具有replication slave 權限。
mysql> CREATE USER repl IDENTIFIED BY 'repl';
mysql> grant replication slave on *.* to 'repl'@'192.168.10.2' identified by 'repl';
在從服務器上進行登錄驗證
[sparkadmin@hadoop4 ~]$ mysql -h192.168.10.1 -urepl -prepl
mysql>
1.2、編輯my.cnf文件
# vim /etc/my.cnf
添加
server-id=1
log-bin=mysql-bin 並開啓log-bin二進制日誌文件
1.3、重啓mysql數據庫
[sparkadmin@hadoop3 ~]$ sudo service mysqld restart
[sparkadmin@hadoop3 ~]$ mysql -uroot -proot
查看權限:
mysql> show grants for 'repl'@'192.168.10.2';
查看binlog 日誌文件名和偏移量
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
2、從服務器
2.1、編輯my.cnf文件
# vim /etc/my.cnf
添加
server-id=2
2.2、重啓mysql數據庫
[sparkadmin@hadoop4 ~]$ sudo service mysqld restart
2.3、對從數據庫進行相應設置
[sparkadmin@hadoop4 ~]$ mysql -uroot -proot
mysql> CHANGE MASTER TO
-> master_host='192.168.10.1',
-> master_user='repl',
-> master_password='repl',
-> master_port=3306,
-> master_log_file='mysql-bin.000007',
-> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.06 sec)
2.4、啓動從服務器slave線程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
執行show processlist命令顯示以下進程:
mysql> show processlist\G
*************************** 5. row ***************************
Id: 16
User: system user
Host:
db: NULL
Command: Connect
Time: 267
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
5 rows in set (0.00 sec)
表示slave已經連接上master,開始接受並執行日誌
2.5、查看slave線程狀態
在從服務器上執行
mysql> show slave status\G;
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
出現上面的信息說明配置成功
測試
1、在主服務器test數據庫中創建user表
mysql>use test;
mysql>create table user(id int);
2、在從服務器中查看user表
mysql>use test;
mysql> show tables like 'user';
+----------------------+
| Tables_in_test(user) |
+----------------------+
| user |
+----------------------+
1 row in set (0.00 sec)
說明主從數據同步成功。
在主服務器上創建數據庫
mysql> create database aa;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aa |
| mysql |
| test |
+--------------------+
在從服務器上查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aa |
| mysql |
| test |