Mysql Master-slave 主從配置

來源:互聯網
上載者:User

標籤:

MySQL主從複製

 

情境描述:
主要資料庫伺服器:192.168.10.130,MySQL已經安裝,並且無應用資料。
從資料庫伺服器:192.168.10.131,MySQL已經安裝,並且無應用資料。

2.1 主伺服器上進行的操作

 

啟動mysql服務

/opt/mysql/init.d/mysql start

通過命令列登入管理MySQL伺服器

/opt/mysql/bin/mysql -uroot -p‘new-password‘

授權給從資料庫伺服器192.168.10.131

mysql> GRANT REPLICATION SLAVE ON *.* to ‘rep1‘@‘192.168.10.131‘ identified by ‘password‘;

查詢主要資料庫狀態

Mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000005 | 261 | | |+------------------+----------+--------------+------------------+

記錄下 FILE 及 Position 的值,在後面進行從伺服器操作的時候需要用到。

2.2 配置從伺服器

 

修改從伺服器的設定檔/opt/mysql/etc/my.cnf

將 server-id = 1修改為 server-id = 10,並確保這個ID沒有被別的MySQL服務所使用。

啟動mysql服務

/opt/mysql/init.d/mysql start

通過命令列登入管理MySQL伺服器

/opt/mysql/bin/mysql -uroot -p‘new-password‘

執行同步SQL語句

mysql> change master tomaster_host=’192.168.10.130’,master_user=’rep1’,master_password=’password’,master_log_file=’mysql-bin.000005’,master_log_pos=261;

 

正確執行後啟動Slave同步進程

mysql> start slave;

 

主從同步檢查

mysql> show slave status\G==============================================**************** 1. row *******************Slave_IO_State:Master_Host: 192.168.10.130Master_User: rep1Master_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000005Read_Master_Log_Pos: 415Relay_Log_File: localhost-relay-bin.000008Relay_Log_Pos: 561Relay_Master_Log_File: mysql-bin.000005Slave_IO_Running: YESSlave_SQL_Running: YESReplicate_Do_DB:……………省略若干……………Master_Server_Id: 11 row in set (0.01 sec)==============================================

 

其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須為YES,才表明狀態正常。

如果主伺服器已經存在應用資料,則在進行主從複製時,需要做以下處理:

(1)主要資料庫進行鎖表操作,不讓資料再進行寫入動作

 

mysql> FLUSH TABLES WITH READ LOCK;

 

(2)查看主要資料庫狀態

mysql> show master status;

 

(3)記錄下 FILE 及 Position 的值。

將主伺服器的資料檔案(整個/opt/mysql/data目錄)複製到從伺服器,建議通過tar歸檔壓縮後再傳到從伺服器解壓。

(4)取消主要資料庫鎖定

mysql> UNLOCK TABLES;
2.3 驗證主從複製效果

 

主伺服器上的操作

在主伺服器上建立資料庫first_db

   mysql> create database first_db;Query Ok, 1 row affected (0.01 sec)

 

在主伺服器上建立表first_tb

mysql> create table first_tb(id int(3),name char(10));Query Ok, 1 row affected (0.00 sec)

 

在主伺服器上的表first_tb中插入記錄

mysql> insert into first_tb values (001,’myself’);Query Ok, 1 row affected (0.00 sec)

 

在從伺服器上查看

mysql> show databases;=============================+--------------------+| Database |+--------------------+| information_schema || first_db || mysql || performance_schema || test |+--------------------+5 rows in set (0.01 sec)=============================

資料庫first_db已經自動產生

 

mysql> use first_dbDatabase chagedmysql> show tables;=============================+--------------------+| Tables_in_first_db |+--------------------+| first_tb |+--------------------+1 row in set (0.02 sec)=============================

資料庫表first_tb也已經自動建立

 

mysql> select * from first_tb;=============================+------+------+| id | name |+------+------+| 1 | myself |+------+------+1 rows in set (0.00 sec)=============================

記錄也已經存在

由此,整個MySQL主從複製的過程就完成了,接下來,我們進行MySQL讀寫分離的安裝與配置。

Mysql Master-slave 主從配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.