MySQL5.7主主復制配置
主機1IP:192.168.1.2
主機2IP:192.168.1.4
一、首先安裝MySQL 5.7
1、卸載兩台主機系統中已經有的mysql相關軟件包
rpm -qa | grep -i mysql
yum remove mysql(相關軟件包名稱) -y
2、分別在兩台主機系統中執行以下命令
在http://dev.mysql.com/downloads/repo/yum/站點下載mysql57-community-release-el6-8.noarch
rpm -ivh mysql57-community-release-el6-8.noarch.rpm
yum -y install mysql.x86_64 mysql-server.x86_64 mysql-devel.x86_64
3、分別在兩台主機上執行以下語句,初始化
mkdir /home/mysql
mysqld --initialize-insecure --user=mysql --datadir=/home/mysql
備注:
如果你用 --initialize 初始化data目錄,請用如下命令登錄服務
mysql -u root -p
執行上面命令,會提示輸入密碼,輸入隨機生成的密碼即可。
如果你不知道這個隨機密碼,請查看error log文件查找這個隨機密碼。
如果用 --initialize-insecure 初始化data目錄,請用root用登錄,並不需要輸入密碼就可以登錄,如下命令:
mysql -u root --skip-password
Mysql5.7忘記root密碼及mysql5.7修改root密碼的方法的相關資料
a、關閉正在運行的 MySQL :service mysql stop
b、運行:mysqld_safe --skip-grant-tables &
c、為了安全可以這樣禁止遠程連接:mysqld_safe --skip-grant-tables --skip-networking &
d、使用mysql連接server:mysql -p
e、更改密碼:
mysql> update mysql.user set authentication_string=password('wclwcw') where user='root' and Host = 'localhost';
特別提醒注意的一點是,新版的mysql數據庫下的user表中已經沒有Password字段了
而是將加密后的用戶密碼存儲於authentication_string字段
4、分別登錄修改主機1、主機2的mysql密碼
輸入mysql回車,進入mysql環境
set password = password ('wclwcw');
flush privileges;
5、在主機1(192.168.1.2)的/etc/my.cnf中配置以下內容
[mysqld]
datadir=/home/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id = 1
auto-increment-increment = 2
auto-increment-offset = 1
explicit_defaults_for_timestamp = 1
character_set_server=utf8
interactive_timeout = 57600
log-bin = mysql-bin
expire-logs-days = 100
replicate-do-db = wang #需要同步的數據庫
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
slave-skip-errors=all
log-slave-updates
symbolic-links=0
skip-name-resolve
6、在主機1(192.168.1.4)的/etc/my.cnf中配置以下內容
[mysqld]
datadir=/home/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id = 2
auto-increment-increment = 2
auto-increment-offset = 2
character_set_server=utf8
interactive_timeout = 57600
log-bin = mysql-bin
expire-logs-days = 100
replicate-do-db = wang #需要同步的數據庫
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
slave-skip-errors=all
log-slave-updates
symbolic-links=0
skip-name-resolve
備注:主機1和主機2都只有server-id不同和 auto-increment- offset不同
auto-increment-offset是用來設定數據庫中自動增長的起點的,回為這兩能
服務器都設定了一次自動增長值2,所以它們的起點必須得不同,這樣才能避
免兩台服務器數據同步時出現主鍵沖突replicate-do-db 指定同步的數據庫,
我們只在兩台服務器間同步wang數據庫
另:auto-increment-increment的值應設為整個結構中服務器的總數,本案例用到兩台服務器,所以值設為2
7、分別重啟主機1,主機2的mysql服務,使配置生效
service mysqld restart
8、在主機1(192.168.1.2)上創建數據庫和表並插入數據
mysql -uroot -pwclwcw
create database wang;
use wang;
create table wclwcw(id int,name varchar(100));
insert into wclwcw value (1,'tom')
9、相互授權
在主機1(192.168.1.2)上
GRANT REPLICATION SLAVE ON *.* TO 'wang'@'192.168.1.4' IDENTIFIED BY 'wclwcw'
flush privileges;
在主機2(192.168.1.4)上
GRANT REPLICATION SLAVE ON *.* TO 'wang'@'192.168.1.2' IDENTIFIED BY 'wclwcw'
flush privileges;
10、互告bin-log信息
a、在主機1(192.168.1.2)上
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000006 | 106 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
b、在主機2(192.168.1.4)上
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000008 | 192 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
c、在主機1(192.168.1.2)上
mysql> change master to master_host='192.168.1.4',master_user='wang',master_password='wclwcw',master_log_file='mysql-bin.000008',master_log_pos=194;
d、在主機2(192.168.1.4)上
mysql> change master to master_host='192.168.1.2',master_user='wang',master_password='wclwcw',master_log_file='mysql-bin.000007',master_log_pos=1082;
11、在主機1、主機2兩服務器都執行以下命令
mysql> start slave;
12、查看狀態
主機1上(192.168.1.2)
mysql> show slave status\G
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.4
Master_User: zz
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 778
Relay_Log_File: template-relay-bin.000002
Relay_Log_Pos: 780
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wang
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 778
Relay_Log_Space: 990
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: e6a7bb8f-4fe6-11e6-abd0-fa163e5cb863
Master_Info_File: /home/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
在主機2(192.168.1.4)上
mysql> show slave status\G;
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: zz
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 769
Relay_Log_File: template-relay-bin.000002
Relay_Log_Pos: 484
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wang
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 769
Relay_Log_Space: 694
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 10824007-4fe4-11e6-a4ff-fa163ea94ff8
Master_Info_File: /home/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
當看到了兩個yes,即:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
說明已經配置成功了,同時查看主機2中是否已經有主機1中的數據庫
其他
同步數據
用test做的實驗,導出將test.sql文件從1.4服務器拷貝到1.2服務器
備份數據前先鎖表,保證數據一致性
mysql> FLUSH TABLES WITH READ LOCK;
# mysqldump -uroot -p123456 test> /tmp/test.sql;
mysql> UNLOCK TABLES;
scp /tmp/test.sql root@192.168.1.2:/tmp
在搭建mysql master-slave復制環境時,一切都正常,但是在slave上執行:show slave status\G; 時,出現下面的狀況:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
一直無法進行正確的復制。查看錯誤日志文件顯示:Connecting error_code:2003
看來是slave 沒有連接上master,在網上搜索一下,網上文章大都只是指出了可能的三種錯誤:
1. 網絡不通
2. 密碼不對
3. pos不對
我測試網絡是可以ping通的。密碼也對,pos也對。
后來想了很久,突然想起來了防火牆好像沒有關閉!一檢查果然。
所以第四種錯誤是:
4. 防火牆沒有關閉。
相關命令如下:
關閉命令: service iptables stop
永久關閉防火牆:chkconfig iptables off
兩個命令同時運行,運行完成后查看防火牆關閉狀態
service iptables status
另外順便也將 selinux關閉掉:
1 vi /etc/selinux/config
2 #SELINUX=enforcing #注釋掉
3 #SELINUXTYPE=targeted #注釋掉
4 SELINUX=disabled #增加
5 :wq #保存,關閉。
6 shutdown -r now #重啟系統
查看SELinux的狀態:
getenforce