mysql資料庫主從同步配置教程--資料庫同步

來源:互聯網
上載者:User

標籤:



背景: 最近有一個mysql資料庫同步的需求,我用了mysql主從同步的方法來實現。下面把步驟記錄一下。


環境和拓撲

作業系統:Centos6.6 X64

mysql版本:5.1.73

Master: 10.6.1.210

Slave:  10.6.1.211

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/82/F6/wKioL1dnh8CwnoQKAAAo92B86eQ742.jpg" title="4C9.jpg" alt="wKioL1dnh8CwnoQKAAAo92B86eQ742.jpg" />



需求: 實現Master上test庫同步到Slave上,但是禁止同步該庫下的AA表


1.配置Master上的my.cnf

#vim  /etc/my.cnf 新增內容到[mysqld]下,設定只同步test 資料庫:

[mysqld]

log-bin=mysql-bin

binlog_format=mixed

binlog_do_db=test

server-id=1

 

2.配置Slave上的my.cnf

#vim  /etc/my.cnf

新增內容到[mysqld]下:

log-bin=mysql-bin

binlog_format=mixed

server-id=10

relay-log =relay-bin

log_slave_updates=1

replicate_ignore_table=AA(忽略同步某個表)

 

3.在Master中建立一個備份帳戶:每個slave使用標準的MySQL使用者名稱和密碼串連Master。

進行複製操作的使用者會授予REPLICATION   SLAVE 許可權。


命令如下:

#建立一個帳戶repluser,並且只能允許10.6.1.211這個主機來登陸,密碼是123456。

mysql>grant  replication client,replication  slave  on  *.*   to ‘repluser‘@‘10.6.1.211‘    identified by  ‘123456‘;


QueryOK,0 rows affected(0.00sec)


mysql>flush privileges;

QueryOK,0 rows affected(0.00sec)

 

4.拷貝資料,保持資料庫內資料一致,新安裝可以忽略此步驟。


備份Master上的test庫,然後複製到從伺服器上.

#mysqldump -u root -p password123   test>/tmp/test.sql

將匯出的資料庫複寫到從伺服器上。

#scp /tmp/test.sql [email protected]:/tmp/

 

在Slave上匯入新的test資料庫。 登陸從後運行

#mysql -u root -p password123 test</tmp/test.sql


5.重啟mysql服務,主從server均要重啟。

#service mysql restart

 

6.查看Master資料庫上的bin檔案以及時間點. 登入Master伺服器的mysql  後執行:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000015 |     2474 | test         |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


此處,bin檔案為mysql-bin.000015,節點為2474。

 

7.啟動從伺服器的中繼日誌,登陸從伺服器的mysql 後執行以下命令,標紅部分為剛才在主伺服器上查詢到的bin  檔案以及節點資訊:


mysql>change master to master_host=‘10.6.1.210‘,master_user=‘repluser‘,

master_password=‘123456‘,master_log_file=‘mysql-bin.000015‘,master_log_pos=2474,

master_connect_retry=5

QueryOK,0 rows affected(0.03sec)

 

#開啟從伺服器節點的複製進程,實現主從複製;

mysql>start slave;

 

#查看從伺服器狀態,主要關注IO線程和SQL  線程的開啟狀況:

mysql>show slave status \G




Slave_IO_Running:Yes            #IO thread  是否運行

Slave_SQL_Running:Yes         #SQL thread是否運行


8.查看主從伺服器上的線程狀態

主伺服器:

 

mysql>show processlist \G

State: Has sent all binlog to slave; waiting for binlog to be updated

 

從伺服器

mysql>show processlist \G

State: Has read all relay log; waiting for the slave I/O thread to update it

至此,mysql資料庫主從同步複製配置完成.

 




驗證

1.  在Master上的test庫下建立一個test表.   插入記錄,然後看看是否同步到了Slave上。


Master上建表

mysql> CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ;


Master上插入記錄

mysql>insert into test(id,name) values(1,‘steven‘);

mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | steven |
+----+--------+
1 row in set (0.00 sec)



Slave上查看是否同步過去.


mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | steven |
+----+--------+
1 row in set (0.00 sec)



2. 在Master上的test庫下建立一個AA表.   插入記錄,然後看看是否同步到了Slave上。

在Master上查詢AA表.

mysql> select * from AA;
+----+--------+
| id | name   |
+----+--------+
|  1 | Angelababy |
+----+--------+
1 row in set (0.00 sec)




Slave上查詢AA記錄是空.

mysql> select * from AA;
Empty set (0.00 sec)







本文出自 “技術成就夢想” 部落格,請務必保留此出處http://pizibaidu.blog.51cto.com/1361909/1791003

mysql資料庫主從同步配置教程--資料庫同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.