參考網站:
http://blog.csdn.net/faye0412/article/details/6280761
http://blog.csdn.net/kk185800961/article/details/49235975
1 MySql主從配置
主庫所在服務器IP地址為192.168.178.2,從庫所在服務器IP地址為192.168.178.3
1.1 MySql主從復制原理
mysql 主從同步原理:
1. master 將操作記錄到二進制日志(binary log)中;
2. slave IO 線程 將master的binary log events讀寫到它的中繼日志(relay log);
3. slave SQL進程讀取中繼日志,將重做記錄數據到數據庫中。

MySQL的主從同步是一個很成熟的架構,優點為:
①在從服務器可以執行查詢工作(即我們常說的讀功能),降低主服務器壓力;
②在從主服務器進行備份,避免備份期間影響主服務器服務;
③當主服務器出現問題時,可以切換到從服務器。
1.2 Mysql的安裝
本文采用yum安裝,命令如下:
[root@xldwhj ~]# yum install mysql mysql-devel mysql-server
1.3 主庫設置
修改主庫my.cnf,主要是設置個不一樣的id和logbin(#這可依具體環境而定,壓力大的化可采用huge.cnf),紅色部分為新增部分。
[root@xldwhj ~]# vim /etc/my.conf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id=1
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
主庫設置(2):
port = 3306
server_id = 1
log-bin= mysql-bin
binlog_format = mixed
read-only=0
#binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=1
auto-increment-increment=2
啟動主庫生效:
[root@xldwhj ~]# service mysqld restart
登陸主庫:
[root@xldwhj ~]# mysql –u root –p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 242
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 all privileges on *.* to '用戶名'@'%' identified by '密碼'; //賦予從庫權限帳號,
本文建立的賬戶名稱為xldwhj,密碼為123123
//(主從庫設置用第二種方式時的設置方法)
mysql>grant replication slave on *.* to '用戶名'@'192.168.1.153' identified by '密碼';
允許用戶在主庫上讀取日志,輸入為從庫設置的用戶名和密碼,本文創建的用戶名為xldroot。
mysql> select user,host from mysql.user; //查看是否創建用戶成功
+-----------+-----------+
| user | host |
+-----------+-----------+
| xldroot | % | //此處為從庫新創建的用戶。
| 用戶名 | % |
| root | 127.0.0.1 |
| | localhost |
| root | localhost |
| | xldwhj |
| root | xldwhj |
+-----------+-----------+
7 rows in set (0.00 sec)
鎖主庫表:
mysql> flush tables with read lock;
#現在禁止操作主數據庫!防止日志有變化,保證主從數據初始狀態一致!
7、顯示主庫信息
記錄File和Position,從庫設置將會用到
mysql> show master status;
+------------------+----------+--------------+---------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+---------------------------------------------------------+
| mysql-bin.000003 | 790 | | information_schema,cluster,mysql ------|
+------------------+----------+--------------+---------------------------------------------------------+
1 row in set (0.00 sec)
至此主庫設置完成
1.4 從庫設置
在主庫服務器上解鎖主庫表
mysql> unlock tables;
修改從庫配置文件/etc/my.cof
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
master-host=192.168.178.2
master-user=root
master-password=pfingo
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
從庫設置(2)
port = 3306
server_id = 2
log-bin= mysql-bin
binlog_format = mixed
read-only=0
#replicate-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on
#auto-increment-offset=2
#auto-increment-increment=2
在從庫服務器上驗證連接主庫:
[root@xldmysql ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 269
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> slave stop;
mysql> change master to master_host='192.168.178.2',master_user='xldroot',master_password='123123',master_log_file='mysql-bin.000003', master_log_pos=790;
//連接MASTER MASTER_LOG_FILE為主庫的File,MASTER_LOG_POS為主庫的Position
//上述第二條命令語句中的master_log_file='mysql-bin.000003', master_log_pos=790;對應為前面在主庫中執行的show master status;結果。
啟動從庫服務
mysql> slave start;
主庫從庫配置文件部分說明:
log-bin :需要啟用二進制日志
server_id : 用於標識不同的數據庫服務器
binlog-do-db : 需要記錄到二進制日志的數據庫
binlog-ignore-db : 忽略記錄二進制日志的數據庫
auto-increment-offset :該服務器自增列的初始值。
auto-increment-increment :該服務器自增列增量。
replicate-do-db :指定復制的數據庫
replicate-ignore-db :不復制的數據庫
relay_log :從庫的中繼日志,主庫日志寫到中繼日志,中繼日志再重做到從庫。
log-slave-updates :該從庫是否寫入二進制日志,如果需要成為多主則可啟用。只讀可以不需要。
如果為多主的話注意設置 auto-increment-offset 和 auto-increment-increment
1.5 測試
在主庫服務器上新建一張數據庫表:
mysql> use test;
Database changed
mysql> CREATE TABLE `myTest` (
-> `id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
-> `username` VARCHAR( 20 ) NOT NULL ,
-> `password` CHAR( 32 ) NOT NULL ,
-> `last_update` DATETIME NOT NULL ,
-> `number` FLOAT( 10 ) NOT NULL ,
-> `content` TEXT NOT NULL ,
-> PRIMARY KEY ( `id` )
-> ) ENGINE = MYISAM ;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| myTest |
+----------------+
1 row in set (0.00 sec)
在從庫查看主庫新建的表是否存在:
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| myTest |
+----------------+
1 row in set (0.01 sec)
可以看出從庫與主庫中信息一致,主從配置成功。
為了更進一步驗證在從庫上輸入show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.178.2
Master_User: xldroot
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
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: 106
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_Running: Yes(網絡正常);
Slave_SQL_Running: Yes(表結構正常)
進一步驗證了以上過程的正確性。
2 MySql主主配置
采用第二種主從復制的my.cnf配置
192.168.178.2虛擬機的my.cnf配置如下:
[mysqld]
port = 3306
server_id = 1
log-bin= mysql-bin
binlog_format = mixed
read-only=0
#binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=1
auto-increment-increment=2
#主主復制的192.168.178.2作為從庫的設置(新增)
#replicate-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on
192.168.178.3虛擬機的my.cnf的設置如下:
[mysqld]
basedir =/usr/local/mysql
datadir =/usr/local/mysql/data
port = 3306
server_id = 2
log-bin= mysql-bin
binlog_format = mixed
read-only=0
#replicate-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on
#主主復制的192.168.178.3作為主庫設置(新增)
#binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=2
auto-increment-increment=2
添加完成后,重啟數據庫
[root@xldwhj ~]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
在192.168.178.3原從庫中創建192.168.178.2原主庫用於連接的用戶:
mysql>grant replication slave on *.* to 'xldroot'@'192.168.1.153' identified by '123123';
查看192.168.178.3原從庫的日志記錄位置:
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 521
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema,performance_schema
1 row in set (0.00 sec)
從192.168.178.2原主庫連接到192.168.178.3原從庫的命令如下:
mysql>
change master to master_host='192.168.178.3',master_user='xldroot',
master_password='123123',master_log_file='mysql-bin.000009', master_log_pos=521;
查看192.168.178.2原主庫的同步信息:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.178.3
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 521
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 499
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema,performance_schema
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: 521
Relay_Log_Space: 654
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)
雙主配置已經配好,下面進行進一步的測試:
在192.168.178.3原從庫上創建一張表並進行相關操作:
mysql>use test
mysql>create table tabdemo(id int primary key auto_increment, value int default 0) auto_increment= 1 engine=innodb default charset=utf8;
mysql>insert into tabdemo(value) values(1),(1),(1),(1),(1);
mysql>select * from tabdemo;
+----+-------+
| id | value |
+----+-------+
| 1 | 1 |
| 3 | 1 |
| 5 | 1 |
| 7 | 1 |
| 9 | 1 |
+----+-------+
在192.168.178.2原主機上執行:
mysql>select * from tabdemo; //出現下述信息
+----+-------+
| id | value |
+----+-------+
| 1 | 1 |
| 3 | 1 |
| 5 | 1 |
| 7 | 1 |
| 9 | 1 |
+----+-------+
執行
mysql> insert into tabdemo(value) values(2),(2),(2),(2),(2);
mysql> select * from tabdemo;
+----+-------+
| id | value |
+----+-------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 2 |
| 8 | 2 |
| 10 | 2 |
| 12 | 2 |
| 14 | 2 |
+----+-------+
在192.168.178.3上執行
mysql> select * from tabdemo;
+----+-------+
| id | value |
+----+-------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 2 |
| 8 | 2 |
| 10 | 2 |
| 12 | 2 |
| 14 | 2 |
+----+-------+
至此為止,測試完畢,雙主配置成功。
