mysql主從同步

192.168.20.63 duwers63.space

192.168.20.60 duwers60.space

關閉iptables selinux

主從數據庫版本一致

拓撲

wKioL1e-ahajxhdLAAByFriC3Ao718.jpg


主庫配置【192.168.20.63

查看版本

[root@duwers63 ~]# mysql -uroot -p123456

mysql> show variables like '%version%';                   #查看版本

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

| Variable_name           | Value               |

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

| protocol_version        | 10                  |

| version                 | 5.1.73-log          |

| version_comment         | Source distribution |

| version_compile_machine | x86_64              |

| version_compile_os      | redhat-linux-gnu    |

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

5 rows in set (0.01 sec)

創建需要同步的庫

mysql> create database duwers63;

mysql> use duwers63

mysql> create table test1(id int);

mysql> show databases;

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

| Database           |

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

| information_schema |

| duwers63           |

| mysql              |

| ucenter            |

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

4 rows in set (0.00 sec)

 

停止mysql服務

[root@duwers63 ~]# service mysqld stop

修改配置文件

[root@duwers63 ~]# vim /etc/my.cnf

添加

log-bin=mysqllog                        #開啓二進制日誌

server-id=63                                #本機數據庫標識

binlog-do-db=duwers63    #二進制要同步的db

 

[root@duwers63 ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

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

user=mysql

# Disabling symbolic-links is recommendedto prevent assorted security risks

symbolic-links=0

log-bin=mysqllog

server-id=63

binlog-do-db=tree

 

[mysqld_safe]

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

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

[root@duwers63 ~]#

重啓mysql服務

[root@duwers63 ~]# service mysqld start

[root@duwers63 ~]# mysql -uroot -p123456

 

授權同步賬戶

mysql> grant replication slave on *.* [email protected] identified by "123456";

查看主庫狀態

mysql> show master status;

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

| File            | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

| mysqllog.000001 |      257 | tree         |                  |

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

1 row in set (0.00 sec)

 

查看二進制日誌狀態

[root@duwers63 ~]# ls /var/lib/mysql/

duwers63 ibdata1  ib_logfile0  ib_logfile1 mysql  mysqllog.000001  mysqllog.index  mysql.sock ucenter

[root@duwers63 ~]#

 

確保同步複製前兩個數據庫的一致性

[root@duwers63 ~]# mysqldump -u root-p123456 -A >all.sql

[root@duwers63 ~]# scp -P 44968 [email protected]:/root

 

從服務器配置【192.168.20.60

[root@duwers60 ~]# service mysqld start

測試連通

[root@duwers60 ~]# mysql -u slave -h192.168.20.63 -p123456 -A

mysql> show databases;

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

| Database           |

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

| information_schema |

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

1 row in set (0.00 sec)

 

導入數據庫,和主數據庫保持一致性

[root@duwers60 ~]# mysql -u root -p</root/all.sql

修改配置文件

[root@duwers60 ~]# vim /etc/my.cnf

添加

server-id=60

master-host=192.168.20.63

master-user=slave

master-password=123456

重啓服務

[root@duwers60 ~]# service mysqld restart

檢查從服務器複製功能狀態

[root@duwers60 ~]# mysql -u root -p123456

 

mysql> show slave status \G

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.20.63

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysqllog.000001

         Read_Master_Log_Pos: 257

               Relay_Log_File:mysqld-relay-bin.000002

                Relay_Log_Pos: 401

       Relay_Master_Log_File: mysqllog.000001

             Slave_IO_Running: Yes                              #負責與主機的IO通信Yes說明從服務器安裝成功

           Slave_SQL_Running: Yes                             #負責自己slave  mysql進程  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: 257

              Relay_Log_Space: 557

              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)

 

測試數據的同步

在主服務器master上寫數據

mysql> use duwers63

mysql> show tables;

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

| Tables_in_duwers63 |

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

| test1              |

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

1 row in set (0.00 sec)

 

mysql> insert into test1 values(1);

 

在從服務器slave上讀數據

 

mysql> use duwers63

mysql> select * from test1;

+------+

| id  |

+------+

|   1 |

+------+

1 row in set (0.00 sec)


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