環境是先前配置lnmp+keepalived負載均衡的2臺機器,IP分別是10.10.10.13和10.10.10.14.php
1,登陸系統mysql
Last login: Sun Jul 24 23:36:55 2016 from 10.10.10.1nginx
[root@hd3 ~]# netstat -nltpsql
Active Internet connections (only servers)服務器
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 負載均衡
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9471/php-fpm ssh
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 9362/mysqld tcp
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11021/nginx ide
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1961/sshd php-fpm
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1821/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2115/master
tcp 0 0 :::22 :::* LISTEN 1961/sshd
tcp 0 0 ::1:631 :::* LISTEN 1821/cupsd
tcp 0 0 ::1:25 :::* LISTEN 2115/master
注:主從機器mysql版本一致,並都已經初始化表並設置了root口令。
2、修改主從服務器配置文件的2個字段
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //啓用二進制日誌
server-id=1 //服務器惟一ID,默認是1,這裏主的用的默認,從服務器改成2。以後
# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
3,在主服務器上創建賬戶並受權slave:
#mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, 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>GRANT REPLICATION SLAVE ON *.* to 'sync'@'%' identified by '123456';
查詢master的狀態,記錄下 FILE 及 Position 的值
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 251 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4,配置從服務器
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, 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> change master to master_host='10.10.10.13',master_user='sync',master_password='123456',master_log_file='my
sql-bin.000002',master_log_pos=251;
Query OK, 0 rows affected (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.10.13
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 251
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 251
Relay_Log_Space: 407
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:
1 row in set (0.00 sec)
關鍵是Slave_IO及Slave_SQL進程必定要都YES
5,主從複製測試
先在主mysql上:
mysql> create database fuck;
Query OK, 1 row affected (0.03 sec)
mysql> use fuck;
Database changed
mysql>
mysql>
mysql> create table fuck(id int(3),name char(10));
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql>
mysql> insert into fuck values(001,'abcd');
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| fuck |
| mysql |
| test |
+--------------------+
4 rows in set (0.03 sec)
mysql>
再登陸從mysql,主服務器上新增的數據都出來了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| fuck |
| mysql |
| test |
+--------------------+
4 rows in set (0.02 sec)
mysql> use fuck;
Database changed
mysql> select * from fuck;
+------+------+
| id | name |
+------+------+
| 1 | abcd |
+------+------+
1 row in set (0.00 sec)
mysql>