mysql主從複製

1、主庫操做mysql

 

1、1查看主庫系統及mysql信息sql



1數據庫

2bash

3ide

4測試

5spa

6orm

[root@db01 ~]# cat /etc/redhat-release server

CentOS release 6.7 (Final)事務

[root@db01 ~]# uname -r

2.6.32-573.el6.x86_64

[root@db01 ~]# mysql --version

mysql  Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.1

 

1、2主庫設置server-id以及開啓binlog

1

2

3

[root@db01 backup]# pwd

/server/backup

[root@db01 backup]# egrep "log-bin|server-id" /etc/my.cnf

server-id       = 51#IP結尾只適用於單實例),其餘從庫的server-id不要跟主庫重複固然全部從庫的也不要重複修改完必定記得重啓庫

log-bin=mysql-bin #主庫開啓binlog

 

1、3登陸主庫

1

[root@db01 ~]# mysql -uroot -poldboy123

 

1、4設置同步用戶

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;

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

| user | host      |

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

| rep  | 10.0.0.%  |

| root | 127.0.0.1 |

| root | ::1       |

|      | db01      |

| root | db01      |

|      | localhost |

| root | localhost |

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

7 rows in set (0.00 sec)

mysql> show grants for rep@'10.0.0.%';

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

| Grants for rep@10.0.0.%                                                                                               |

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

| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'10.0.0.%' IDENTIFIED BY PASSWORD '*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515' |

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

1 row in set (0.00 sec)

 

1、5主庫鎖表(此時不能執行事務處理)

1

2

mysql> flush table with read lock;

Query OK, 0 rows affected (0.00 sec)

 

1、6查看主庫binlog信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000002 |      289 |              |                  |

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

1 row in set (0.00 sec)

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| testdb01           |

| testdb02           |

| testdb03           |

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

7 rows in set (0.00 sec)

 

1、7另開窗口導出主庫現有數據

1

2

3

4

[root@db01 backup]# mysqldump -uroot -poldboy123 --events -A -B|gzip>/server/backup/mysql_bak.2016-09-16.sql.gz 

[root@db01 backup]# ll

total 144

-rw-r--r-- 1 root root 144465 Sep 16 17:38 mysql_bak.2016-09-16.sql.gz

 

1、8發送給從庫

1

[root@db01 backup]# scp mysql_bak.2016-09-16.sql.gz root@10.0.0.52:/server/backup/

1、9主庫關閉鎖表狀態

1

2

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

 

2、從庫操做

 

2、1查看從庫系統及mysql信息

1

2

3

4

5

6

7

8

[root@db02 ~]# cat /etc/redhat-release 

CentOS release 6.7 (Final)

[root@db02 ~]# uname -r

2.6.32-573.el6.x86_64

[root@db02 backup]# mysql --version

mysql  Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.1

[root@db02 backup]# pwd

/server/backup

 

2、2設置從庫server-id

1

[root@db02 backup]# egrep "log-bin|server-id" /etc/my.cnf

server-id       = 52  #IP結尾只適用於單實例),從庫的server-id必定不要跟主庫和其餘庫重複修改必定要重啓數據庫

#log-bin=mysql-bin    #從庫必定不要開啓binlog

 

2、3在從庫對應解壓主庫發送的數據庫備份文件

1

2

3

4

5

6

7

[root@db02 backup]# ll

total 144

-rw-r--r-- 1 root root 144465 Sep 16 17:39 mysql_bak.2016-09-16.sql.gz

[root@db02 backup]# gzip -d mysql_bak.2016-09-16.sql.gz 

[root@db02 backup]# ll

total 520

-rw-r--r-- 1 root root 529834 Sep 16 17:39 mysql_bak.2016-09-16.sql

 

2、4從庫導入主庫對應數據庫數據

1

2

[root@db02 backup]# mysql -uroot -poldboy123 < mysql_bak.2016-09-16.sql 

[root@db02 ~]# mysql -uroot -poldboy123

 

2、5從庫查看導入主庫數據結果

1

2

3

4

5

6

7

8

9

10

11

12

13

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| testdb01           |

| testdb02           |

| testdb03           |

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

7 rows in set (0.00 sec)

 

2、6設置從庫同步主庫數據命令(主從複製關鍵命令)

mysql> CHANGE MASTER TOMASTER_HOST='10.0.0.51',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=289,MASTER_USER='rep',MASTER_PASSWORD='oldboy123';

Query OK, 0 rows affected (0.03 sec)

#注意這裏的MASTER_LOG_FILE='mysql-bin.000002'和MASTER_LOG_POS=289是對應上述主庫在鎖表時查看主庫狀態信息的內容,對應「一、6查看主庫binlog信息」。

 

2、7必定要先執行上述change master再start slave

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

mysql> start slave;

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

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 289

               Relay_Log_File: db02-relay-bin.000002

                Relay_Log_Pos: 253

        Relay_Master_Log_File: mysql-bin.000002

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

              Relay_Log_Space: 408

              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: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 51

1 row in set (0.00 sec)

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| testdb01           |

| testdb02           |

| testdb03           |

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

7 rows in set (0.00 sec)

 

3、主庫和從庫測試

 

#測試主庫數據是否同步到了從庫

3、1主庫操做

1

2

3

4

5

6

7

8

9

mysql> create database testdb04;

Query OK, 1 row affected (0.00 sec)

mysql> show databases like 'testdb04';

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

| Database (testdb04) |

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

| testdb04            |

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

1 row in set (0.00 sec)

 

3、2從庫查看

1

2

3

4

5

6

7

mysql> show databases like 'testdb04';

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

| Database (testdb04) |

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

| testdb04            |

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

1 row in set (0.00 sec)

 

#現主從同步測試成功。

相關文章
相關標籤/搜索