mysql數據庫主備同步部署

數據庫主從同步部署mysql


約定:sql

主數據庫master  ip:10.0.0.4:3306數據庫

備數據庫slave   ip:10.0.0.3:3306app


部署流程:tcp

主數據庫操做以下ide

一、開啓主數據的binlog參數,和設置server-id測試

二、建立用於同步數據的帳號repthis

三、鎖表並查看當前日誌名稱和位置spa

四、備份當前主數據庫的所有數據(全備)rest

五、給主數據解鎖。讓主庫繼續提供服務

六、繼續往主數據寫數據


備數據庫操做

一、把主數據備份的全備數據備份到備數據庫

二、在備數據庫上設置主從同步的相關信息,如主數據庫的IP、端口、同步帳號、密碼、binlog文件名、binlog位置點

三、開始主從同步start slave;

四、查看是否備份成功,show slave status\G;

############################################################################







主數據庫上面操做

開啓主數據庫的binlog參數,和設置server-id

egrep "server-id|log-bin" /etc/my.cnf 

########################################

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

server-id       = 1

log-bin=mysql-bin

[root@M_MYSQL ~]#


重啓3306實例數據庫

/etc/init.d/mysqld restart

netstat -lntup|grep 3306

########################################

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

Shutting down MySQL. SUCCESS! 

Starting MySQL.. SUCCESS! 

[root@M_MYSQL ~]# netstat -lntup|grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2412/mysqld         

[root@M_MYSQL ~]#

########################################

查看是否記錄bin_log日誌

[root@M_MYSQL ~]# ll /application/mysql/data/

total 28700

-rw-rw----. 1 mysql mysql 18874368 Sep 30 21:58 ibdata1

-rw-rw----. 1 mysql mysql  5242880 Sep 30 21:58 ib_logfile0

-rw-rw----. 1 mysql mysql  5242880 Sep 30 21:33 ib_logfile1

-rw-r-----. 1 mysql root      3458 Sep 30 21:58 M_MYSQL.err

-rw-rw----. 1 mysql mysql        5 Sep 30 21:58 M_MYSQL.pid

drwx------. 2 mysql root      4096 Sep 30 21:33 mysql

-rw-rw----. 1 mysql mysql      107 Sep 30 21:58 mysql-bin.000001          ##有這個,證實在記錄主從同步

-rw-rw----. 1 mysql mysql       19 Sep 30 21:58 mysql-bin.index

drwx------. 2 mysql mysql     4096 Sep 30 21:33 performance_schema

drwxr-xr-x. 2 mysql mysql     4096 Sep 30 21:33 test

[root@M_MYSQL ~]# 

########################################

##在主數據庫中建立測試用的數據庫和表。

create database lvnian;

use lvnian;

create table test(

id int(4) not null auto_increment,

name char(20) not null,

primary key(id)

);

flush privileges;

mysql -uroot -plvnian -e "use lvnian; select * from test;"

for n in `seq 100`;do mysql -uroot -plvnian -e "use lvnian;insert test values($n,'lvnian$n'); " ;done;

mysql -uroot -plvnian -e "use lvnian; select * from test;"

############################################


##############

查看是否開啓binlog

[root@M_MYSQL ~]#  mysql -uroot -plvnian -e "show variables like 'log_bin'"                     

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

| Variable_name | Value |

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

| log_bin       | ON    |

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

[root@M_MYSQL ~]# 

############################################

建立用於同步數據的帳號rep

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

Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql -uroot -plvnian -e "select user,host from mysql.user;" 

########################################

[root@M_MYSQL ~]#  mysql -uroot -plvnian -e "select user,host from mysql.user;" 

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

| user | host      |

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

| rep  | 10.0.0.%  |

| root | 127.0.0.1 |

############################################

鎖表


mysql> flush table with read lock; 

Query OK, 0 rows affected (0.00 sec)

################################################################################

查看當前日誌名稱和位置

mysql -uroot -plvnian -e " show master status;"  

mysql -uroot -plvnian -e "show master logs;" 


########################################

[root@M_MYSQL ~]# mysql -uroot -plvnian -e " show master status;"  

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000003 |      302 |              |                  |

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

[root@M_MYSQL ~]# mysql -uroot -plvnian -e "show master logs;"     

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

| Log_name         | File_size |

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

| mysql-bin.000001 |     20802 |

| mysql-bin.000002 |       150 |

| mysql-bin.000003 |       302 |

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

[root@M_MYSQL ~]# 


######################################

備份當前主數據庫數據

另開一個窗口

 mysqldump -uroot  -plvnian -A -B -F --master-data=2 | gzip > /tmp/mysql_bak.$(date +%F).sql.gz        

 

################## 

[root@M_MYSQL ~]#  mysqldump -uroot  -plvnian -A -B -F --master-data=2 --events | gzip > /tmp/mysql_bak.$(date +%F).sql.gz 

[root@M_MYSQL ~]# ll /tmp/

total 148

-rw-r--r--. 1 root  root  145225 Sep 30 22:19 mysql_bak.2015-09-30.sql.gz

[root@M_MYSQL ~]#


給主數據庫解鎖

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)


###############################################

繼續往主數據庫寫數據

 for n in `seq 100 150`;do mysql -uroot -plvnian -e "use lvnian;insert test values($n,'lvnian$n'); " ;done;

 mysql -uroot -plvnian -e "use lvnian; select * from test;"


############################################################################

############################################################################

############################################################################

############################################################################


啓動備數據庫

 /etc/init.d/mysqld restart


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

Shutting down MySQL. SUCCESS! 

Starting MySQL.. SUCCESS! 

[root@S_MYSQL ~]# netstat -lntup|grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2475/mysqld         

[root@S_MYSQL ~]#

#####################################################

把備份數據備份到備數據庫

scp root@10.0.0.4:/tmp/mysql_bak.2015-09-30.sql.gz .

#####################################

[root@S_MYSQL ~]# scp root@10.0.0.4:/tmp/mysql_bak.2015-09-30.sql.gz .

root@10.0.0.4's password: 

mysql_bak.2015-09-30.sql.gz                                   100%  142KB 141.8KB/s   00:00    

[root@S_MYSQL ~]# ls

anaconda-ks.cfg  aq  deng.txxt  install.log  install.log.syslog  mysql_bak.2015-09-30.sql.gz

[root@S_MYSQL ~]# 

##########

[root@S_MYSQL ~]# gzip -d mysql_bak.2015-09-30.sql.gz 

[root@S_MYSQL ~]# mysql -uroot -plvnian < mysql_bak.2015-09-30.sql 

[root@S_MYSQL ~]# mysql -uroot -plvnian -e "show databases;"       

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

| Database           |

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

| information_schema |

| lvnian             |

| mysql              |

| performance_schema |

| test               |

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

[root@S_MYSQL ~]# mysql -uroot -plvnian -e "use lvnian;select * from test;"

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

| id  | name      |

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

|   1 | lvnian1   |

|   2 | lvnian2   |

|   3 | lvnian3   |

|   4 | lvnian4   |

|   5 | lvnian5   |

|   6 | lvnian6   |

|   7 | lvnian7   |

|   8 | lvnian8   |

|   9 | lvnian9   |

|  10 | lvnian10  |

|  11 | lvnian11  |

|  12 | lvnian12  |

|  13 | lvnian13  |

....

....

....


所有恢復到備數據庫成功

###############################################


開始設置主從同步

###################################

更加主數據庫剛纔的文件名稱和位置點,等到下面的內容

CHANGE MASTER TO

MASTER_HOST='10.0.0.4',

MASTER_PORT=3306,

MASTER_USER='rep',

MASTER_PASSWORD='lvnian123456',

MASTER_LOG_FILE='mysql-bin.000003',

MASTER_LOG_POS=302;

######################

start slave;

show  slave status\G;

###################################

mysql> CHANGE MASTER TO

    -> MASTER_HOST='10.0.0.4',

    -> MASTER_PORT=3306,

    -> MASTER_USER='rep',

    -> MASTER_PASSWORD='lvnian123456',

    -> MASTER_LOG_FILE='mysql-bin.000003',

    -> MASTER_LOG_POS=302;

Query OK, 0 rows affected (0.08 sec)


mysql> 


mysql> start slave;

Query OK, 0 rows affected (0.03 sec)


mysql> show  slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 10.0.0.4

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000004

          Read_Master_Log_Pos: 9759

               Relay_Log_File: S_MYSQL-relay-bin.000005

                Relay_Log_Pos: 9905

        Relay_Master_Log_File: mysql-bin.000004

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

              Relay_Log_Space: 10106

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

1 row in set (0.00 sec)


ERROR: 

No query specified


mysql> 

成功的表現:

[root@S_MYSQL ~]# mysql -uroot -plvnian -e "show  slave status\G;"|egrep "Slave_IO_Runnin|Slave_SQL_Running"

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

[root@S_MYSQL ~]#

出現上面兩個yes證實數據庫主從同步成功了


################################################################################

小結

注意防火牆問題

注意主從數據庫的server-id問題,主從數據庫的server-id必須是不一樣的。

注意主數據庫的配置文件my.cnf 必須開啓記錄binlog日誌




################################################################################

同步故障解決


下面主從不一樣步的緣由就是上面從庫的server-id沒有修改,致使主從數據庫的server-id是同樣的,就會出現下面錯誤:

注意錯誤提示:

         Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).




mysql> show  slave status\G;

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

               Slave_IO_State: 

                  Master_Host: 10.0.0.4

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 302

               Relay_Log_File: S_MYSQL-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: No

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

              Relay_Log_Space: 107

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

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 1593

                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

               Last_SQL_Errno: 0

               Last_SQL_Error: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 1

1 row in set (0.00 sec)



上面同步不成功的緣由是。沒有修改備數據庫的server-id .致使備數據庫和主數據庫的server-id相同。把主備數據庫的server-id改成不一致就能夠了

相關文章
相關標籤/搜索