mysql主從和主主備份

來源:互聯網
上載者:User

標籤:mysql

  



650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/49/A7/wKioL1QX1QTybsV8AABAsFd9QfY119.jpg" title="選區_062.png" alt="wKioL1QX1QTybsV8AABAsFd9QfY119.jpg" />

1.建立一個資料庫用於測試同步;

mysql> create database dragon;

Query OK, 1 row affected (0.04 sec)


mysql> use dragon

Database changed

mysql> create table user(id int(5),name char(20));

Query OK, 0 rows affected (0.17 sec)


mysql> quit


2.修改主庫my.cnf主要設定個不一樣的ID,以及同步的資料庫的名字,我一般用vim 來完成,vim /etc/my.cnf 在[mysqld]中加入內容如下

server-id=1

log-bin=binlog

binlog-do-db=dragon  #指明同步那些資料庫

replicate-ignore-db=mysql  #指明不同步那些資料庫

replicate-ignore-db=information_schema

port=3306


重啟服務使得設定檔生效

[[email protected] ~]# service mysqld restart

Shutting down MySQL.                                       [確定]

Starting MySQL..                                           [確定]

[[email protected] ~]# mysql -uroot -p123


3.登入主庫賦予從庫許可權帳號,允許使用者在主庫上讀取日誌(使用者名稱: admin密碼:123456)

[[email protected] ~]# mysql -uroot -p123

mysql> grant replication slave on *.* to ‘admin‘@‘10.10.10.2‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.01 sec)


在slave上測試帳號是否能夠被登陸:

[[email protected] ~]# mysql -uadmin -p123456 -h 10.10.10.1 -S/tmp/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.33-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> 


4.slave機器擷取mastre快照。有兩種方法:一種是進入/var/lib/mysql/用tar方法備份

[[email protected] mysql]# cd /usr/local/mysql/data/

[[email protected] data]# tar czvf dragon.tar.gz dragon/

dragon/

dragon/db.opt

dragon/user.frm

[[email protected] data]# scp dragon.tar.gz 10.10.10.2:/usr/local/mysql/data

The authenticity of host ‘10.10.10.2 (10.10.10.2)‘ can‘t be established.

RSA key fingerprint is 01:77:02:41:8b:f3:86:3e:e9:58:b3:f2:91:34:91:90.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added ‘10.10.10.2‘ (RSA) to the list of known hosts.

[email protected]‘s password: 

dragon.tar.gz                                 100%  416     0.4KB/s   00:00   


[[email protected] data]# tar zxf dragon.tar.gz 

[[email protected] data]# service mysqld restart


5.在master1上查看dragon資料檔案的資訊(記錄file、position,從庫設定將會用到),並且鎖住表;

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.01 sec)


mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000024 |      563 | dragon       |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)


mysql> unlock tables;


6.修改slave2的設定檔vim /etc/my.cnf,把server_id改為2或者添加server_id=2,總之server_id是一個與master的server_id不同數值即可,如果server_id與master相同會出現1593這個錯誤,可以進入資料庫通過" show variables like "server_id""來查看servver_id是否被更改. 進入slave的資料庫進行下一步同步配置。


mysql> slave stop;

Query OK, 0 rows affected (0.00 sec)


mysql>change master to master_host=‘10.10.10.1‘,master_user=‘admin‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000024 ‘,master_log_pos=563;


mysql> slave start;

Query OK, 0 rows affected (0.00 sec)


重啟一下資料庫:service mysqld restart;再進入資料庫查看同步是否成功,slave_io_running和slave_sql_running均為YES。

mysql>show slave status\G;

Slave_IO_Running: Yes


Slave_SQL_Running: Yes


在master1上建立一個表,再到slave上查看錶有沒有被同步到。

mysql> create table dragonttest(id int(4),name char(10));

Query OK, 0 rows affected (0.18 sec)


mysql> 


mysql> show tables;

+------------------+

| Tables_in_dragon |

+------------------+

| dragonttest      |

| user             |

+------------------+

2 rows in set (0.00 sec)



配置主主備份:

  

    這裡才是主主複製的開始,其實方法很簡單,我們之前可以實現主從複製也就是說,再把從做為主,主再做為從,就實現主主複製了,我是在主從複製完成後在此基礎上去完成主主複製的。


1、編輯之前的從伺服器,也就是現在的master2

vim /etc/my.cnf

 加入如下內容:

binlog-do-db=dragon


replicate-ignore-db=test


replicate-ignore-db=mysql


replicate-ignore-db=information_schema


port=3306


2.重啟服務

 service mysqld restart

 

3、登入master2(master2 Ip地址為10.10.10.2)資料庫賦予master資料庫許可權帳號,允許使用者在master主庫(ip 為10.10.10.1)上讀取日誌(使用者名稱: admin密碼:123456)

mysql -uroot -p123

grant replication slave on *.* to ‘admin‘@‘10.10.10.1‘identified by ‘123456‘;

為驗證帳號我們可以在master1的機器上用命令作如下測試

mysql -u admin -p -h 10.10.10.2 -S/tmp/mysql.sock


4、master2上登入資料庫記錄file 和position

mysql> show master status;


+---------------+----------+--------------+------------------+


| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |


+---------------+----------+--------------+------------------+


| binlog.000004 |      106 | dragon   |                  |


+---------------+----------+--------------+------------------+


1 row in set (0.00 sec)


5、在master上登入資料庫做如下配置

mysql>stop slave;

mysql>CHANGE MASTER TO MASTER_HOST=‘10.10.10.2‘,MASTER_USER=‘admin‘,MASTER_PASSWORD=‘123456‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘binlog.000004 ‘,MASTER_LOG_POS=106;        (請注意CHANGE 到pos=106為一行命令)

mysql>start slave;

mysql>show slave status\G;

Slave_IO_Running: Yes


Slave_SQL_Running: Yes

看到上兩個進程為資料庫與另一邊的master 已經建立串連


本文出自 “龍愛雪琪” 部落格,請務必保留此出處http://dragon123.blog.51cto.com/9152073/1553276

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.