mysql-5.5配置主從 及 主主關係

來源:互聯網
上載者:User

標籤:搭建mysql主從

前言:Mysql在生產環境中的宕機率特別的高,一般我們在該機器上只安裝mysql,不作他用。資料庫本     身也是極易產生瓶頸的地方。Master:主(用於寫入資料) Slave:從(用於讀取資料),也是     實現讀寫分離的重要舉措。國內很多的ApsaraVideo for Live網站、bilibili等彈幕類的網站,在視頻架構上     做了個透明的彈幕,       後台資料庫每秒發生數以萬計的讀寫操作,後台營運人員真是想     死的心都有。。。哈哈。。廢話不多說,開始部署……


準備前的步驟:關閉selinux,關閉防火牆、已編譯好的mysql5.5

實驗環境:Centos6.5 IP:192.168.1.1 Mysql主

      Centos6.5 IP:192.168.1.2 Mysql從

配置主

[[email protected] ~]# mysql -u root -predhat  //mysql登入使用者及密碼

[[email protected] ~]# show dabaseses;  //顯示資料庫

[[email protected] ~]# use mysql;    //使用mysql庫

[[email protected] ~]# show tables;   //顯示表

[[email protected] ~]# desc user;    //查看user表的結構

[[email protected] ~]# select Host,User,Password form mysql.user; //在user表中查詢主機,使用者密碼欄位

[[email protected] ~]# insert into mysql.user(Host,User,Password) values(‘localhost‘,‘baidu‘,password(‘123.com‘));  //插入一條資料,使用者百度,密碼123.com

[[email protected] ~]# grant replication slave on *.* to ‘baidu‘@‘192.168.1.2‘ identified by ‘123.com‘ with grant option;  //授權給192.168.1.2使用者可以使用百度這個使用者,密碼123.com來進行複製操作

[[email protected] ~]# create database test; //建立測試的庫,庫名為test

[[email protected] ~]# vim /etc/my.cnf

大約49行log-bin=mysql-bin處

添加:binlog-do-db=test       //將test庫寫入二進位日誌

    binlog-ignore-db=mysql   //忽視mysql庫寫入二進位日誌

修改:server-id = 1    每台做主從或主主的server id的值應唯一。

[[email protected] ~]# /etc/init.d/mysqld restart

[[email protected] ~]# mysql -u root -predhat

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000005 |      107 | test         | mysql            |

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

1 row in set (0.00 sec) //上面兩個值將在從Mysql中用到。。

下面配置從mysql

[[email protected] ~]# vim /etc/my.cnf

大約49行log-bin=mysql-bin處

添加:replicate-do-db=test    //複製的庫名test

    replicate-ignore-db=mysql //不複製的庫名mysql

修改:server-id = 2    值唯一

[[email protected] ~]# /etc/init.d/mysqld restart

[[email protected] ~]# mysql -u root -predhat

[[email protected] ~]# create database test; //建立和主上面一樣的同步的庫名

[[email protected] ~]# slave stop;  

[[email protected] ~]# change master to master_host=‘192.168.1.1‘, master_user=‘baidu‘, master_password=‘123.com‘, master_log_file=‘mysql-bin.000005‘, master_log_pos=107;

//指定主的IP,可複製使用者及密碼。上面兩個值在每次重啟主Mysql都會變。如果重啟,則需要重新change master to 了

[[email protected] ~]# slave start;  //開啟複製

[[email protected] ~]# show slave status\G 

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.1

                  Master_User: baidu

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 107

               Relay_Log_File: localhost-relay-bin.000003

                Relay_Log_Pos: 253

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: test

          Replicate_Ignore_DB: mysql

兩個Yes表示主從配置正確……

接下來我們到主mysql的test庫上面建立一個表,再到從mysql上面看看是否同步過來了。


主mysql

mysql> use test;

Database changed

mysql> create table student(id int(11) not null, primary key(id));

Query OK, 0 rows affected (0.06 sec)


mysql> show tables;

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

| Tables_in_user |

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

| student        |

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

1 row in set (0.00 sec)


從mysql

mysql> use test;

Database changed

mysql> show tables;

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

| Tables_in_user |

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

| student        |

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

1 row in set (0.00 sec)

可以看到student表已經成功同步過來了,至此Mysql主從實驗已完成。 

PS:常見錯誤,防火牆阻塞了連接埠,授權密碼填寫錯誤   mysql-bin錯誤    每次重啟服務會變更        change mater to   需要重新敲。

Mysql編譯安裝的都有一個小小的BUG,不管什麼錯誤導致mysql重啟失敗都會報PID丟失,一般都是你的設定檔寫錯了,或者尋找記錄檔進行拍錯。。

本文出自 “11000174” 部落格,請務必保留此出處http://11010174.blog.51cto.com/11000174/1901854

mysql-5.5配置主從 及 主主關係

聯繫我們

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