進擊的Mysql——Slave環境搭建及配置

一)環境拓撲

   今天討論的是mysql集羣這一塊,資源如下所示:

二)Mysql安裝配置

   這裏不一一贅述,可以選擇源代碼編譯安裝,也可以參考我之前的一篇博文“懶人“速成——linux LAMP環境。

三)節點配置

主節點:

   1.首先在數據庫中建立2個數據庫和表:

#service mysqld start
#mysql
mysql>create database www;
mysql>use www;
mysql>create table www(id int);
mysql>insert into www values(1);
mysql> select * from www;

   查看數據:

   同理創建blog數據庫和表:

mysql>create database blog;
mysql>use blog;
mysql>create table blog(id int);
mysql>insert into blog values(1);
mysql> select * from blog;


2.修改My.cnf配置文件

vi etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin          //日誌爲2進制,不需要更改
server-id    =1            //爲1就是Master,不需要更改
binlog-do-db=blog          //需要同步的庫
binlog-do-db=www           //需要同步的庫
binlog-ignore-db=mysql,test,information_schema   //不需要記錄日誌的數據庫名,多個數據庫中間用逗號(,)隔開
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
innodb_data_home_dir = /usr/local/mysql/data/      //innodb的表空間位置
innodb_data_file_path = ibdata1:50M:autoextend     //表空間的名字,開始50M
innodb_log_group_home_dir = /usr/local/mysql/data/
innodb_buffer_pool_size = 256M                     //爲系統內存的50-80%
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

   3.重啓數據庫

[root@Test01 /]# service mysqld restart


Stopping mysqld:  [  OK  ]
Starting mysqld:  [  OK  ]

4.創建有權限的賬號,讓Slave數據庫訪問主數據


[root@Test01 /]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.69-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>



mysql> grant replication slave on *.* to repl@"192.168.1.26" identified by '123456';
Query OK, 0 rows affected (0.00 sec)



mysql>flush privileges;
;

注意:格式:GRANT REPLICATION SLAVE ON *.* TO '帳號'@'從服務器IP或主機名' IDENTIFIED BY '密碼';

 5.備份Master數據庫


mysql> flush tables with read lock;  //不要退出這個終端,否則這個鎖就不生效了。
同時取得快照,並記錄日誌和偏移量:
mysql> show master status;
+------------------+----------+--------------+-------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB              |
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000001 |      196 | blog,www     | mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)

6.開啓另一個終端,對主數據庫做快照


[root@Test01 mysql]#cd /var/lib/mysql/
[root@Test01 mysql]#tar -zcvf backup.tar.gz www blog

 7.庫解鎖


mysql> unlock tables;

從節點:

   1.安裝mysql,方法同上,不在贅述。

   2.修改My.cnf配置文件


[root@Test02 mysql]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id  = 2
#master-host     =   192.168.1.10
#master-user     =   root
#master-password =   584911644
#master-port     =  3306
#master-log-pos =   196
#master-log-file = mysql-bin.000001
#master-connect-retry=60
replicate-do-db=www        //告訴slave只做www數據庫的更新
replicate-do-db=blog       //告訴slave只做blog數據庫的更新
log-slave-updates
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
"/etc/my.cnf" 23L, 551C

3.把之前主數據庫壓縮的數據拷貝到對應位置

我這裏用的是SecureFX,當然,方法很多,也不細說了,可謂是條條大路通羅馬。

4.啓動數據庫


[root@Test02 mysql]# service mysqld start


Starting mysqld:  [  OK  ]

5.配置,啓動Slave

注意:Slave_IO_Running是否要從 Master Server 復製 Binary Log 資料,必須爲 YesSlave_SQL_Running是否要執行從 Master Server 復製過來的 Binary Log 資料,必須爲 Yes

mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='192.168.1.10',master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=196;
Query OK, 0 rows affected (0.04 sec)
mysql> slave start;
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.1.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 339
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 394
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: www,blog
          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: 339
              Relay_Log_Space: 550
              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)

四)後期測試

主節點-數據增加:


mysql>  insert into www values(2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from www;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

從節點數據同步驗證:


mysql> select * from www;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

主節點-數據刪除:


mysql> delete from www  where id ='2';
Query OK, 1 row affected (0.04 sec)
mysql> select * from www;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

從節點數據同步驗證:


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


綜述:

   Test02,從數據庫上能做到同步數據庫的更新操作,目前是WWW和blog兩張表。當然,這裏只是拋磚引玉的作用,大家可以舉一反三,根據自己實際需求,做出最好的環境搭建和配置。

   最近在弄RHCS和keepalived,閒來有空就記錄幾筆數據庫slave配置,方便大家學習和自己的積累。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章