Mysql主從複製

                                   Mysql主從複製mysql

1、    功能:

能夠當作一種備份方式sql

用來實現讀寫分離,緩解單個數據庫的壓力數據庫

2、    備份原理

Master 上提供binlog服務器

    slave    經過 I/O線程從 master拿取 binlog,並複製到slave的中繼日誌中 slave    經過 SQL線程從 slave的中繼日誌中讀取binlog ,而後解析到slave socket

3、    簡單部署實驗:

實驗環境:ide

 

主服務器:mysql_1172.16.114.15/24測試

 

從服務器:mysql_2172.16.114.16/24spa

 

1.     在主服務器上配置:

編輯mysql配置文件vi /etc/my.cnf線程

 

 [root@mysql_1 ~]# cat /etc/my.cnfrest

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links isrecommended to prevent assorted security risks

symbolic-links=0

server-id = 1

log-bin=loyu-bin   #添加這兩行內容

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

 

重啓mysqld服務

 

[root@mysql_1 ~]# /etc/init.d/mysqld restart

 

建立受權用戶loyu

 

[root@mysql-1 ~]# mysql

……………

mysql>show master status;

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

| File            | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

|loyu-bin.000003 |      106 |              |                  |

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

1 row in set (0.00 sec)

mysql> grant replication slave on *.* to 'loyu'@'172.16.114.%' identified by'123456';

Query OK, 0 rows affected (0.00sec)

 

受權後登陸從服務器測試鏈接,若是鏈接不上須要立刻檢查問題,而不是繼續作下去。否則也會失敗

 

[root@mysql-2 ~]# mysql -u loyu -h 172.16.114.15 -p

Enterpassword:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.73-log Sourcedistribution

 

Copyright (c) 2000, 2013, Oracleand/or its affiliates. All rights reserved.

 

Oracle is a registered trademarkof Oracle Corporation and/or its

affiliates. Other names may betrademarks of their respective

owners.

 

Type 'help;' or '\h' for help.Type '\c' to clear the current input statement.

mysql>

 

 

2.     在從服務器上配置:

編輯mysql配置文件vi /etc/my.cnf

 

[root@mysql-2 ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links isrecommended to prevent assorted security risks

symbolic-links=0

server-id= 2   #添加這行內容

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

重啓mysqld服務

 

[root@mysql-2 ~]# /etc/init.d/mysqld restart

 

在從庫上指定主庫的鏈接信息,並開啓同步進程

 

[root@mysql-2 ~]# mysql

…………………..

mysql> CHANGE MASTER TO MASTER_HOST='172.16.114.15', MASTER_PORT=3306,MASTER_USER='loyu', MASTER_PASSWORD='123456', MASTER_LOG_FILE='loyu-bin.000003', MASTER_LOG_POS=106;

mysql>start slave;

Query OK, 0 rows affected (0.00sec)

mysql> show slave status\G

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 172.16.114.15

                  Master_User: loyu

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: loyu-bin.000003

          Read_Master_Log_Pos: 259

               Relay_Log_File:mysqld-relay-bin.000002

                Relay_Log_Pos: 403

        Relay_Master_Log_File: loyu-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          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: 259

              Relay_Log_Space: 559

              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)

能夠看到

Slave_IO_Running: Yes (主從I/O正確)        

Slave_SQL_Running: Yes(主從進程正確)

 

 

測試主從數據同步:在主服務器(172.16.114.15)上操做;

建立數據庫,建立表

 

mysql>create database loyu;

QueryOK, 1 row affected (0.00 sec)

 

mysql>show databases;

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

|Database           |

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

| information_schema|

|loyu               |

|mysql              |

|test               |

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

4 rowsin set (0.00 sec)

 

mysql>use loyu;

Databasechanged

mysql>create table loyu(id int(3),name char(10));

QueryOK, 0 rows affected (0.03 sec)

 

mysql> insert into loyu values (001,'loyu_mysql');

QueryOK, 1 row affected (0.00 sec)

 

 

在從服務器中查看是否有loyu數據庫和表

能夠看到是成功複製過來了

wKioL1XOqWCwbO8-AALElfuHJ4E921.jpg

相關文章
相關標籤/搜索