http://ywliyq.blog.51cto.com/11433965/1856963
本文爲南非螞蟻的書籍《循序漸進linux-第二版》-8.3.5的讀筆記
mysql雙主互備架構圖
mysql主主互備模式配置
環境:
DB1:主服務器 centos6.6 mysql5.1.73
IP:10.24.24.111
DB2:從服務器 centos6.6 mysql5.1.73
IP:10.24.24.112
mysql VIP:10.24.24.112
----------------------------------------
centos6.x安裝mysql
# yum -y install mysql mysql-server
centos7.x安裝mariaDB
# yum -y install mariadb-server mariadb
安裝完成後目錄結構如下:
啓動mysql
# /etc/init.d/mysqld start
創建mysql密碼:(jzh0024)
# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
#這裏輸入目前mariadb數據庫的root密碼,默認是空
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
#這裏詢問是否設置mariadb數據庫root的密碼,輸入"y"給用戶設置一個新的密碼
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y #這裏詢問是否刪除匿名用戶,輸入"y"刪除
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y #這裏詢問是否關閉root用戶遠程登錄權限,輸入"y"
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #這裏詢問是否刪除測試數據庫及其權限,輸入"y"
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #這裏詢問是否重新載入授權表,輸入"y"
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@localhost ~]#
至此,mysql數據庫安裝完成。
-----------------------------------------
1.修改mysql配置文件
DB1 /etc/my.cnf配置,[mysqld]段添加:
server-id = 1
log-bin=mysql-bin
replay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
DB /etc/my.cnf配置,[mysqld]段添加:
server-id = 2
log-bin=mysql-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
這裏需要注意的是,不要在主庫上使用binlog-do-db或binlog-ignore-db選項,也不要在從庫上使用replication-db-do或replication-db選項,因爲這樣可能產生跨庫更新失敗的問題;
推薦從庫上使用replicate_wild_do_table和replicate-wild-ignore-table兩個選項來解決複製過濾問題
2.手動配置數據庫
DB1先創建一個數據庫及表,用於同步測試
mysql> create database ywadmin;
mysql> use ywadmin;
創建表
mysql>
create table personal(member_no char(9) not null,name char(5),birthday
date,exam_score tinyint,primary key(member_no));
查看錶內容
mysql> desc personal;
+------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| member_no | char(9) | NO | PRI | NULL | |
| name | char(5) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| exam_score | tinyint(4) | YES | | NULL | |
+------------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
DB1進行鎖表並備份數據庫
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
不要退出終端,否則鎖表失敗;新開啓一個終端對數據進行備份,或者使用mysqldump進行備份
# cd /var/lib/
# tar zcvf mysql.tar.gz mysql
# scp -P50024 mysql.tar.gz [email protected]:/var/lib/
[email protected]'s password:
mysql.tar.gz 100% 213KB 213.0KB/s 00:00
注意:此處需要開啓DB2授權root遠程登錄
# vim /etc/ssh/sshd_config
#PermitRootLogin no
數據傳輸到DB2後,依次重啓DB1,DB2的數據庫
[root@DB1 ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@DB2 ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
3.創建複製用戶並授權
DB1上創建複製用戶,
mysql> grant replication slave on *.* to 'repl_user'@'10.24.24.112' identified by 'repl_password';
Query OK, 0 rows affected (0.00 sec)
刷新授權表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 271 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
然後在DB2的數據庫中將DB1設爲自己的主服務器
# cd /var/lib/
# tar xf mysql.tar.gz
mysql> change master to \
-> master_host='10.24.24.111',
-> master_user='repl_user',
-> master_password='repl_password',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=271;
需要注意master_log_file和master_log_pos選項,這兩個值是剛纔在DB1上查詢到的結果
DB2上啓動從服務器,並查看DB2上的從服務器運行狀態
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.24.24.111
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 271
Relay_Log_File: mysql-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: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 271
Relay_Log_Space: 406
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)
ERROR:
No query specified
至此,DB1到DB2的MYSQL主從複製已完成。
驗證數據的完整性
DB1上插入數據
mysql> use ywadmin;
mysql> show tables;
+-------------------+
| Tables_in_ywadmin |
+-------------------+
| personal |
+-------------------+
1 row in set (0.00 sec)
mysql> insert into personal values ('001','netseek','1983-03-15','95');
mysql> insert into personal values ('002','heihei','1982-02-24','90');
mysql> insert into personal values ('003','gogo','1985-05-21','85');
mysql> insert into personal values ('004','haha','1984-02-25','84');
mysql> insert into personal values ('005','linlin','1982-04-28','85');
mysql> insert into personal values ('006','xinxin','1985-03-15','75');
mysql> desc personal;
DB2數據庫上驗證數據是否同步
mysql> use ywadmin;
mysql> select * from personal;
+-----------+-------+------------+------------+
| member_no | name | birthday | exam_score |
+-----------+-------+------------+------------+
| 001 | netse | 1983-03-15 | 95 |
| 002 | heihe | 1982-02-24 | 90 |
| 003 | gogo | 1985-05-21 | 85 |
| 004 | haha | 1984-02-25 | 84 |
| 005 | linli | 1982-04-28 | 85 |
| 006 | xinxi | 1985-03-15 | 75 |
+-----------+-------+------------+------------+
6 rows in set (0.00 sec)
數據已完成複製.
---------------------------------------------
配置DB2到DB1的主從複製
DB2數據庫中創建複製用戶
mysql> grant replication slave on *.* to 'repl_user1'@'10.24.24.111' identified by 'repl_password1';
刷新授權表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 273 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在DB1的數據庫中將DB2設爲自己的主服務器
mysql> change master to \
-> master_host='10.24.24.112',
-> master_user='repl_user1',
-> master_password='repl_password1',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=273;
在DB1上啓動從服務器
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看DB1上從服務器的運行狀態
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.24.24.112
Master_User: repl_user1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 273
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000003
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: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 273
Relay_Log_Space: 406
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)
ERROR:
No query specified
Slave_IO_Running和Slave_SQL_Running都處於YES狀態。表明DB1上覆制服務運行正常,mysql雙主模式主從複製配置完畢。
------------------------------------
驗證數據的完整性
DB2上創建新數據庫、表
mysql> create database ywadmin001;
mysql> use ywadmin001;
創建表
mysql>
create table personal001(member_no char(9) not null,name001
char(5),birthday001 date,exam_score001 tinyint,primary key(member_no));
查看錶內容
mysql> desc personal001;
+---------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------+------+-----+---------+-------+
| member_no | char(9) | NO | PRI | NULL | |
| name001 | char(5) | YES | | NULL | |
| birthday001 | date | YES | | NULL | |
| exam_score001 | tinyint(4) | YES | | NULL | |
+---------------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
vmysql> use ywadmin001;
mysql> insert into personal001 values ('001','netseek','1983-03-15','95');
mysql> insert into personal001 values ('002','heihei','1982-02-24','90');
mysql> insert into personal001 values ('003','gogo','1985-05-21','85');
mysql> select * from personal001;
+-----------+---------+-------------+---------------+
| member_no | name001 | birthday001 | exam_score001 |
+-----------+---------+-------------+---------------+
| 001 | netse | 1983-03-15 | 95 |
| 002 | heihe | 1982-02-24 | 90 |
| 003 | gogo | 1985-05-21 | 85 |
+-----------+---------+-------------+---------------+
3 rows in set (0.00 sec)
並在personal表中插入數據
mysql> use ywadmin;
mysql> show tables;
mysql> insert into personal values ('007','ywadmin','1987-11-07','100');
mysql> insert into personal values ('008','ywliyq','1986-12-25','99');
mysql> insert into personal values ('009','xiaxia','1990-12-27','97');
DB1數據庫上驗證數據是否同步
新的數據庫及表是否被創建
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ywadmin |
| ywadmin001 |
+--------------------+
4 rows in set (0.00 sec)
mysql> use ywadmin001;
mysql> show tables;
+----------------------+
| Tables_in_ywadmin001 |
+----------------------+
| personal001 |
+----------------------+
1 row in set (0.00 sec)
mysql> select * from personal001;
+-----------+---------+-------------+---------------+
| member_no | name001 | birthday001 | exam_score001 |
+-----------+---------+-------------+---------------+
| 001 | netse | 1983-03-15 | 95 |
| 002 | heihe | 1982-02-24 | 90 |
| 003 | gogo | 1985-05-21 | 85 |
+-----------+---------+-------------+---------------+
3 rows in set (0.00 sec)
新建庫、表中的內容已同步。
原表插入的數據是否同步
mysql> use ywadmin;
mysql> select * from personal;
+-----------+-------+------------+------------+
| member_no | name | birthday | exam_score |
+-----------+-------+------------+------------+
| 001 | netse | 1983-03-15 | 95 |
| 002 | heihe | 1982-02-24 | 90 |
| 003 | gogo | 1985-05-21 | 85 |
| 004 | haha | 1984-02-25 | 84 |
| 005 | linli | 1982-04-28 | 85 |
| 006 | xinxi | 1985-03-15 | 75 |
| 007 | ywadm | 1987-11-07 | 100 |
| 008 | ywliy | 1986-12-25 | 99 |
| 009 | xiaxi | 1990-12-27 | 97 |
+-----------+-------+------------+------------+
9 rows in set (0.00 sec)
原表插入的列也已同步,數據已完成複製.
刪除DB2上的庫
mysql> drop database ywadmin001;
DB1上檢查ywadmin001庫是否被刪除
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ywadmin |
+--------------------+
3 rows in set (0.00 sec)
刪除很快,基本上是實時同步的.
========================================================