mysql主從備份功能配置與測試

       在高訪問量服務環境下,單機配置mysql服務將無法滿足頻繁高速的數據讀寫操作。一旦mysql出現問題造成數據丟失,無法恢復。因此,在mysql服務上啓用主從備份功能,支持讀寫分離技術。最靠可的是搭建負載均衡分佈式數據庫系統,更加可靠、穩定。


1、搭建環境

兩臺centos機器,安裝mysql服務以及其他依賴包,一臺是主服務器(master),另一臺是從服務器(salve),本節以下操作在兩臺服務器都執行一遍:
192.168.213.135(master)
192.168.213.136(salver)

安裝mysql,命令行輸入:
yum install mysql*

等待安裝結束,啓動mysql服務:
[liang@localhost Desktop]$ sudo service mysqld restart

打印如下即啓動成功:
[sudo] password for liang: 
Stopping mysqld:                                           [  OK  ]
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK


To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:


/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'


Alternatively you can run:
/usr/bin/mysql_secure_installation


which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.


See the manual for more instructions.


You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &


You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl


Please report any problems with the /usr/bin/mysqlbug script!


                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]


[liang@localhost Desktop]$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 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管理員默認空密碼,mysql命令行輸入:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');

打印如下即更改密碼成功:
Query OK, 0 rows affected (0.00 sec)

退出數據庫:
mysql> quit

重新使用新的密碼root權限登錄數據庫:
[liang@localhost Desktop]$ sudo mysql -uroot -p
Enter password:

輸入密碼再次進入mysql管理客戶端界面添加mysql用戶:
mysql> CREATE USER liang IDENTIFIED BY '123456';

爲新用戶創建數據庫:
mysql> create database liang;

設置用戶登錄、操作權限,允許本機以liang賬號登陸,操作liang數據庫的所用表:
mysql> grant all privileges on liang.* to liang@localhost identified by '123456';

此時,退出mysql的管理員賬號,是liang賬號登陸,查看liang賬號是否可以操作liang數據庫,liang登錄數據庫,登錄成功:
[liang@localhost html]$ sudo mysql -uliang -p
[sudo] password for liang: 

登錄成功打印如下:
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 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.

查看可用數據庫,可以看到liang數據:
mysql> show databases;

打印如下包括前面添加的liang數據庫:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liang              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

選擇使用liang數據庫:
mysql> use liang;

創建一個數據庫表:
mysql> create table osinfo (id int primary key,os char(20),ttl int check(ttl>0));

插入數據:
mysql> insert into osinfo(id,os,ttl) values(1,'win10',128);
mysql> insert into osinfo(id,os,ttl) values(2,'ubuntu15',64);

查詢數據表:
mysql> select * from osinfo;

打印如下信息,出現前面插入數據項:
+----+----------+------+
| id | os       | ttl  |
+----+----------+------+
|  1 | win10    |  128 |
|  2 | ubuntu15 |   64 |
+----+----------+------+
2 rows in set (0.00 sec)

2、配置master

編輯數據庫配置文件,命令行輸入:
[root@localhost Desktop]# vi /etc/my.cnf

添加內容,保存退出:
##############################
server-id = 1
log-bin=mysql-bin
binlog-do-db = liang
binlog-ignore-db = mysql,test,information_schema
##############################

重啓mysql服務:
[root@localhost Desktop]# service mysqld restart

打印如下信息,即配置文件沒有出錯,重新啓動mysql成功:
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                          

master授權從slave主從備份權限,root權限登錄服務器mysql,輸入以下命令:
mysql> grant replication slave on *.* to 'liang'@'192.168.213.136' identified by '123456';

刷新權限列表,使剛纔配置權限生效:
mysql> flush privileges;

從slave測試遠程登陸master上的mysql:
[root@localhost Desktop]# mysql -h 192.168.213.135 -u liang -p

輸入密碼登錄成功打印如下,擁有了遠程備份的權限:
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.73 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> 


3、配置slave

配置數據庫配置文件,命令行輸入如下:
[root@localhost Desktop]# vi /etc/my.cnf

添加內容,保存退出:
#################################
server-id=2
master-host= 192.168.213.135
master-port=3306
master-user=liang
master-password=123456
replicate-do-db=liang
master-retry-count = 999
master-connect-retry = 60
#################################

重啓mysql服務:
[root@localhost Desktop]# service mysqld restart

打印如下內容即重啓服務成功:
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

4、驗證主從備份狀態

查看master狀態,master主機上,root權限登錄mysql,輸入如下sql語句:
mysql> show master status;

打印下面內容,File和Position對應的值,以後可以配置slave用於master_log_file、master_log_pos,剛安裝的mysql可以跳過:
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    4
Current database: *** NONE ***


+------------------+----------+--------------+-------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB              |
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000001 |      106 | liang        | mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)

查看slave狀態,slave主機上,root權限登錄mysql,輸入如下sql語句:
mysql> show slave status;


打印如下內容:
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+
| Slave_IO_State                                        | Master_Host     | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File          | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error |
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+
| Waiting to reconnect after a failed master event read | 192.168.213.135 | liang       |        3306 |            60 |                 |                   4 | mysqld-relay-bin.000001 |             4 |                       | No               | Yes               | liang           |                     |                    |                        |                         |                             |          0 |            |            0 |                   0 |             106 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |             0 |               |              0 |                |
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+
1 row in set (0.00 sec)

Slave_SQL_Running、Slave_IO_Running 爲 YES 即主從備份開啓。但是這裏Slave_IO_Running爲 NO,所以現在主從備份並沒有開啓。兩種原因造成當前錯誤:程序可能在slave上進行了寫操作,也可能是slave機器重起後,事務回滾造成的,一般是後者。


嘗試上面的解決方法,命令行輸入,驗證主從備份功能:
mysql> show slave status\G;

打印如下,Slave_IO_Running: Yes,Slave_SQL_Running: Yes,基本配置成功:
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.213.135
                  Master_User: liang
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 382
               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: liang
          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: 382
              Relay_Log_Space: 828
              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)


5、防火牆配置

master命令行打開tcp的3306端口:
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

slave命令行打開tcp、udp的3306端口:
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p udp --dport 3306 -j ACCEPT

6、測試主從備份功能

主服務器上使用liang用戶在liang數據庫中插入新數據:
mysql> insert into osinfo(id,os,ttl) values(3,'win7',128);

主服務器上查詢數據::
mysql> select * from osinfo;

出現上面插入的數據,即插入數據成功:
+----+----------+------+
| id | os       | ttl  |
+----+----------+------+
|  2 | ubuntu15 |   64 |
|  1 | win10    |  128 |
|  3 | win7     |  128 |
+----+----------+------+
3 rows in set (0.00 sec)

從服務器上查詢數據:
mysql> select * from osinfo;

出現上面插入的數據,即主從備份功能配置成功:
+----+----------+------+
| id | os       | ttl  |
+----+----------+------+
|  2 | ubuntu15 |   64 |
|  1 | win10    |  128 |
|  3 | win7     |  128 |
+----+----------+------+
3 rows in set (0.00 sec)


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