国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL5.6主從復制(mysql數據同步配置)

2024-07-24 13:11:21
字體:
來源:轉載
供稿:網友

規劃

master 192.168.56.2

slave  192.168.56.5

1、在主庫上,設置一個復制使用的賬戶rep1,并授予REPLICATION SLAVE權限。

mysql> grant replication slave on *.* to 'rep1'@'192.168.56.2' identified by '123456';Query OK, 0 rows affected (0.01 sec)

2、修改主數據庫服務器的配置文件my.cnf,開啟BINLOG,并設置server-id的值。這兩個參數的修改需要重新啟動數據庫服務才可以生效。

 vi /etc/my.cnf

 [mysqld]

 log-bin=/home/mysql/log/mysql-bin.log

 server-id=1

[root@rhel6 ~]# service mysql restartShutting down MySQL..                                      [  OK  ]Starting MySQL.                                            [  OK  ]

3、在主庫上,設置讀鎖定有效,這個操作是為了確保沒有數據庫操作,以便獲得一個一致性的快照。

mysql> flush tables with read lock;Query OK, 0 rows affected (0.02 sec)

4、得到主庫上當前的二進制日志名和偏移量值。這個操作的目的是為了從數據庫啟動以后,從這個點開始進行數據的恢復。

mysql> show master status;+-----------------+----------+--------------+------------------+-------------------+| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+-----------------+----------+--------------+------------------+-------------------+| mysqlbin.000021 |      120 |              |                  |                   |+-----------------+----------+--------------+------------------+-------------------+1 row in set (0.03 sec)

5、現在主數據庫服務器已經停止了更新操作,需要生成主數據庫的備份,備份的方式有很多種,可以直接在操作系統下復制全部的數據文件到從數據庫服務器上,也可以通過mysqldump導出數據或者使用ibbackup工具進行數據庫的備份。如果主數據庫的服務可以停止,那么直接復制數據文件應該是最快的生成快照的方法:

[root@rhel6 lib]# tar -zcvf mysql.tar.gz mysqlmysql/tar: mysql/mysql.sock: socket ignoredmysql/mysqlbin.000019mysql/test1/mysql/test1/db.optmysql/test1/pack.MYDmysql/test1/myisam2.frmmysql/test1/emp.ibdmysql/test1/mer_myisam.MRGmysql/test1/emp.frmmysql/test1/pri_t.TRG....傳到從數據庫[root@rhel6 lib]# scp mysql.tar.gz root@192.168.56.5:/rootroot@192.168.56.5's password: mysql.tar.gz                                                                                                                                                                    100% 1402KB   1.4MB/s   00:00

6、主數據庫備份完畢后可恢復寫操作,剩下的只需要在從庫上執行

mysql> unlock tables;Query OK, 0 rows affected (0.02 sec)

7、將主數據庫的一致性備份恢復到從數據庫上。如果是使用.tar.gz打包的文件包,只需要解開到相應的目錄即可。

tar -zxvf mysql.tar.gz

8、修改從數據庫的配置文件my.cnf,增加server-id參數。注意server-id的值必須是唯一的,不能和主數據庫的配置相同,如果有多個從數據庫服務器,每個從數據庫服務器必須有自己唯一的server-id值。

 vi my.cnf

 [mysqld]

 server-id=2

9、在從庫上,使用--skip-slave-start選項啟動數據庫,這樣不會立即啟動從數據庫服務上的復制進程,方便我們對從數據庫的服務進行進一步的配置(可選)

 mysqld_safe --skip-slave-start &

 或者修改my.cnf,添加skip-slave-start參數,service mysql start

10、對從數據庫服務器做相應設置,指定復制使用的用戶,主數據庫服務器的IP、端口以及開始執行復制的日志文件和位置等

mysql> change master to     ->  master_host='192.168.56.2',    ->  master_user='rep1',    ->  master_port=3306,    ->  master_password='123456',    ->  master_log_file='mysqlbin.000021',    ->  master_log_pos=120;Query OK, 0 rows affected, 2 warnings (0.08 sec)

11、在從庫上,啟動slave線程

mysql> start slave;Query OK, 0 rows affected (0.17 sec)

12、從庫上查看進程

mysql> show processlist /G;*************************** 1. row ***************************     Id: 1   User: root   Host: localhost     db: zxCommand: Query   Time: 0  State: init   Info: show processlist*************************** 2. row ***************************     Id: 4   User: system user   Host:      db: NULLCommand: Connect   Time: 1484  State: Waiting for master to send event   Info: NULL*************************** 3. row ***************************     Id: 5   User: system user   Host:      db: NULLCommand: Connect   Time: 739  State: Slave has read all relay log; waiting for the slave I/O thread to update it   Info: NULL3 rows in set (0.00 sec)mysql> show slave status /G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.56.2                  Master_User: rep1                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysqlbin.000021          Read_Master_Log_Pos: 652               Relay_Log_File: rhel6-relay-bin.000002                Relay_Log_Pos: 814        Relay_Master_Log_File: mysqlbin.000021             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: 652              Relay_Log_Space: 987              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: 0Master_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                  Master_UUID: 3743271b-aa6d-11e6-aa2e-080027e5f5dd             Master_Info_File: /mysqldata/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set:                 Auto_Position: 01 row in set (0.00 sec)

Slave_IO_Running和Slave_SQL_Running全部為YES才算搭建成功。如果出錯查看報錯原因。我搭建時遇到的兩個錯誤:

(1)Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

原因:搭建時把主庫整個目錄復制過來的,主庫和從庫的UUID一致了,修改從庫的UUID即可。

方法:修改$datadir/auto.cnf,按照16進制格式隨便改一下,重啟mysql即可。

(2)Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

原因:配置change master to時把master_log_file配置錯誤

方法:change master to master_log_file='mysqlbin.000021';start slave;即可。

13、測試復制的正確性

主庫mysql> create database zx;Query OK, 1 row affected (0.05 sec)mysql> use zx;Database changedmysql> create table t(id int);Query OK, 0 rows affected (0.09 sec)mysql> insert into t values(1),(2),(3);Query OK, 3 rows affected (0.01 sec)Records: 3  Duplicates: 0  Warnings: 0從庫mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || data               || mysql              || performance_schema || test               || test1              || zx                 |+--------------------+7 rows in set (0.00 sec)mysql> use zxReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+| id   |+------+|    1 ||    2 ||    3 |+------+3 rows in set (0.00 sec)

注:相關教程知識閱讀請移步到MYSQL教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天等县| 民乐县| 青岛市| 崇礼县| 瑞安市| 屏东县| 南宁市| 西丰县| 东方市| 永靖县| 府谷县| 历史| 高唐县| 连州市| 攀枝花市| 怀仁县| 霸州市| 苗栗市| 靖远县| 聂拉木县| 工布江达县| 呼伦贝尔市| 政和县| 绥芬河市| 永寿县| 弥勒县| 黄浦区| 阿拉善右旗| 思茅市| 八宿县| 镇安县| 孟津县| 饶河县| 铁力市| 皮山县| 兴文县| 邢台市| 金华市| 民权县| 扶风县| 大港区|