mysql主從複製資料庫

來源:互聯網
上載者:User

標籤:空閑   個數   test   主伺服器   server-id   my.cnf   mys   配置   primary   

mysql主從複製相信已經用得很多了,但是由於工作原因一直沒怎麼用過。趁著這段時間相對空閑,也就自己實現一遍。儘管互連網上已有大把類似的文章,但是自身實現的仍然值得記錄。

環境:

主伺服器:centos 6.0 mysql 5.1.67-log IP:192.168.0.107
從伺服器:centos 6.0 mysql 5.1.67-log IP:192.168.0.109
主伺服器test資料庫

CREATE TABLE `menber` (`name` varchar(255) DEFAULT NULL default ‘‘,`id` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;insert into `menber` (`name`, `id`) values(‘zhangsan‘,‘1‘);insert into `menber` (`name`, `id`) values(‘lisi‘,‘2‘);insert into `menber` (`name`, `id`) values(‘王五‘,‘3‘);

mysql預設設定檔,如不特殊指定預設為/etc/my.cnf

mysql設定檔尋找順序:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

配置:

一、主伺服器

1.1、建立一個複製使用者,具有replication slave 許可權。

mysql>grant replication slave on *.* to ‘repl‘@‘192.168.0.109‘ identified by ‘repl‘;mysql>flush privileges;

1.2、編輯my.cnf檔案

vi /etc/my.cnf

添加

  server-id=107

並開啟log-bin二進位記錄檔(Mysql需要有/var/lib/mysql/目錄的讀寫權限【可通過chown -R mysql:mysql /var/lib/mysql命令變更】

  log-bin=/var/lib/mysql/mysql-bin

#指定絕對路徑,否者會出現mysql運行show master status;時無法查看日誌情況mysql> show master status;Empty set (0.00 sec)mysql> show binary logs;ERROR 1381 (HY000): You are not using binary logging

其他擴充配置項: 

binlog-do-db=mysql1 #需要備份的資料庫名,如果備份多個資料庫,重複設定這個選項 即可
binlog-ignore-db=mysql2 #不需要備份的資料庫名,如果備份多個資料庫,重複設定這 個選項即可
log-slave-updates=1 #這個參數一定要加上,否則不會給更新的記錄些到二進位檔案 裡
slave-skip-errors=1 #是跳過錯誤,繼續執行複製操作(可選)

1.3、重啟mysql資料庫

service mysqld restart

1.4、設定讀鎖

mysql>flush tables with read lock;

1.5、得到binlog記錄檔名和位移量(此處記住File名稱和Position值,後面slave伺服器配置時需要用到

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

    

1.6、備份要同步的資料庫

mysqldump test > test.sql

1.7、解鎖

mysql>unlock tables;

    

二、從伺服器(192.168.0.109)

將master(192.168.0.107)備份的資料庫資料恢複到slave從伺服器(192.168.0.109)

2.1、編輯my.cnf檔案

vi /etc/my.cnf

添加

server-id=109

2.2、重啟從資料庫

service mysqld restart

2.3、對從資料庫進行相應設定

  此處要注意logfile的名稱和position的值,其餘host、user和password為主要資料庫設定的帳號和密碼

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to 
   -> master_host=‘192.168.0.107‘,
   -> master_user=‘repl‘,
   -> master_password=‘repl‘,
   -> master_log_file=‘mysql-bin.000001‘,
   -> master_log_pos=713;

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: 192.168.0.107
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
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: 1079
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)

ERROR: 
No query specified

在這裡主要是看:

  Slave_IO_Running=Yes
  Slave_SQL_Running=Yes

如果出現Slave_IO_Running: No或Slave_SQL_Running: NO,需要重做2.3、對從資料庫進行相應設定

三、測試:
  上述項配置完以後可查看master和slave上線程的狀態。在master上,你可以看到slave的I/O線程建立的串連:在master上輸入show processlist\G;

mysql> show processlist\G;*************************** 1. row ***************************     Id: 4   User: root   Host: localhost     db: NULLCommand: Query   Time: 0  State: NULL   Info: show processlist*************************** 2. row ***************************     Id: 19   User: repl   Host: 192.168.0.109:42337     db: NULLCommand: Binlog Dump   Time: 183  State: Has sent all binlog to slave; waiting for binlog to be updated   Info: NULL2 rows in set (0.00 sec)ERROR: No query specified

  3.1、在主要資料庫:192.168.0.107上添加新資料

insert into `menber` (`name`) values(‘李八‘);insert into `menber` (`name`) values(‘蒼井空‘);

3.2從資料庫:192.168.0.109上查看資料庫

mysql> select * from menber;+-----------+----+| name      | id |+-----------+----+| zhangsan  |  1 || lisi      |  2 || 王五    |  3 || 李八    |  4 || 蒼井空 |  5 |+-----------+----+5 rows in set (0.02 sec)

  此時資料已經成功複製到slave從資料庫192.168.0.109上。

mysqldump -ubackup -p123456 -h 192.168.1.2 backup_test > D:\bak\bakcup.sql

在這段命令中:

mysqldump 是myqldump的程式名了;

-u 是資料庫的使用者名稱,後面緊跟著使用者名稱backup;

-p 是資料庫的密碼,後面同樣緊跟著密碼,注意是-p和密碼之間不能有空格;

-h 是資料庫的地址,如果沒有此項表明是備份本地的資料庫;

backup_test 要備份的資料庫名稱;

> 表明該Database Backup到後面的檔案,緊跟當然是備份檔案的地址了,注意要用絕對檔案路徑,檔案的尾碼也可以用.txt。

一旦執行該命令後,mysqldump立即執行備份操作。遠程備份資料庫要似乎網路的情況,一般遠程備份需要一定的時間的。
其實只要記住兩條命令就好

匯出:
(整個資料庫)
mysqldump -u root -p database > data.sql
(單個表)
mysqldump -u root -p database tablename > data.sql

匯入:
mysql -u root -p database < data.sql

 

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.