偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

MySQL主從復(fù)制配置詳解

數(shù)據(jù)庫(kù) MySQL
主服務(wù)器的UUID和從服務(wù)器的UUID重復(fù),因?yàn)槲沂前惭b成功一臺(tái)數(shù)據(jù)庫(kù)后直接克隆的,所以他們的UUID是一樣的,就會(huì)報(bào)這個(gè)錯(cuò)??梢孕薷囊幌聫膸?kù)的UUID即可。

[[404379]]

本文轉(zhuǎn)載自微信公眾號(hào)「SQL數(shù)據(jù)庫(kù)開發(fā)」,作者 丶平凡世界。轉(zhuǎn)載本文請(qǐng)聯(lián)系SQL數(shù)據(jù)庫(kù)開發(fā)公眾號(hào)。

之前很多小伙伴想知道MySQL主從復(fù)制的配置步驟,今天它來(lái)了。帶著你可能碰到的各種異常來(lái)了。

配置環(huán)境

操作系統(tǒng):兩臺(tái)CentOS 7.6的Linux系統(tǒng)

數(shù)據(jù)庫(kù)版本:MySQL 5.6.39

主服務(wù)器IP:192.168.0.1

從服務(wù)器IP:192.168.0.2

安裝數(shù)據(jù)庫(kù)

之前已經(jīng)給小伙伴們?cè)敿?xì)的講解了CentOS安裝MySQL的操作步驟了,還沒看過(guò)的小伙伴可以戳這里:

《Linux環(huán)境下安裝MySQL步驟詳解》

配置前提

1、需要保證3306端口開啟或關(guān)閉防火墻,在MySQL的安裝里有介紹。

2、兩臺(tái)服務(wù)器之間可以相互ping通

  1. --在192.168.0.2上輸入ping命令 
  2. ping 192.168.0.1 
  3. --在192.168.0.1上輸入ping命令 
  4. ping 192.168.0.2 

3、安裝成功一臺(tái)MySQL后,使用虛擬機(jī)克隆一臺(tái)作為從服務(wù)器

配置主(Master)數(shù)據(jù)庫(kù)

1、修改數(shù)據(jù)庫(kù)配置文件

  1. [root@localhost ~]# vi /etc/my.cnf 

將里面的內(nèi)容修改為

  1. [mysqld] 
  2. #開啟二進(jìn)制日志 
  3. log-bin=mysql-bin 
  4. #標(biāo)識(shí)唯一id(必須),一般使用ip最后位 
  5. server-id=1 
  6. #不同步的數(shù)據(jù)庫(kù),可設(shè)置多個(gè) 
  7. binlog-ignore-db=information_schema 
  8. binlog-ignore-db=performance_schema 
  9. binlog-ignore-db=mysql 
  10. #指定需要同步的數(shù)據(jù)庫(kù)(和slave是相互匹配的),可以設(shè)置多個(gè) 
  11. binlog-do-db=test 

添加日志存儲(chǔ)方式和規(guī)則(選填)

  1. #設(shè)置存儲(chǔ)模式不設(shè)置默認(rèn) 
  2. binlog_format=MIXED 
  3. #日志清理時(shí)間 
  4. expire_logs_days=7 
  5. #日志大小 
  6. max_binlog_size=100m 
  7. #緩存大小 
  8. binlog_cache_size=4m 
  9. #最大緩存大小 
  10. max_binlog_cache_size=521m 

注:日志的存儲(chǔ)容量我設(shè)置的都比較小,當(dāng)然你可以根據(jù)實(shí)際情況修改得大一點(diǎn)。

2、重啟數(shù)據(jù)庫(kù)服務(wù)mysqld

  1. service mysqld restart 

如果你按照上面的正確安裝mysql了,這里是可以正常重啟的。如果啟動(dòng)不正常出現(xiàn)如下報(bào)錯(cuò):

The server quit without updating PID file......

你需要使用如下命令查看是否還存在mysqld進(jìn)程

  1. ps -ef|grep mysqld 

如果有,可以使用命令:kill -9 mysqld的進(jìn)程號(hào) 結(jié)束它,然后重新啟動(dòng)mysqld

我就遇到過(guò)上述的情況。當(dāng)然也有其他原因,這里貼一個(gè)其他可能原因的解決辦法供參考:

https://javawind.net/p141

3、登陸MySQL數(shù)據(jù)庫(kù)允許從庫(kù)獲得主庫(kù)日志

  1. [root@localhost ~]# mysql -u root -p 

注:第一次登陸是不需要輸入root的密碼的。

進(jìn)入后做如下配置:

  1. #給從庫(kù)放權(quán)限 
  2. mysql>GRANT FILE ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root password'; #創(chuàng)建用戶 
  3. mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root password'; #修改用戶權(quán)限 
  4. mysql>select host ,user ,password from mysql.user; #查看是否修改成功 
  5. mysql>FLUSH PRIVILEGES; #刷新權(quán)限 

4、重啟MySQL服務(wù),登錄MySQL,查看主庫(kù)信息

  1. [root@localhost ~]# service mysqld restart #重啟mysql服務(wù) 
  2. [root@localhost ~]# mysql -u root -p #登陸mysql 
  3. mysql> show master status; #查看master狀態(tài) 

顯示大概如下內(nèi)容

  1. +------------------+----------+--------------+----------------------------------+-------------------+ 
  2. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | 
  3. +------------------+----------+--------------+----------------------------------+-------------------+ 
  4. | mysql-bin.000006 |    120 | ufind_db | information_schema,performance_schema,mysql | | 
  5. +------------------+----------+--------------+----------------------------------+-------------------+ 
  6. 1 row in set (0.00 sec) 

注:如果執(zhí)行這個(gè)步驟始終為Empty set(0.00 sec),那說(shuō)明前面的my.cnf沒配置對(duì),請(qǐng)回去重新檢查配置步驟。

配置從(Slave)數(shù)據(jù)庫(kù)

1、修改從庫(kù)的數(shù)據(jù)庫(kù)配置文件

  1. [root@localhost ~]# vi /etc/my.cnf 

將里面的內(nèi)容修改為

  1. #開啟二進(jìn)制日志 
  2. log-bin=mysql-bin 
  3. server-id=2 
  4. binlog-ignore-db=information_schema 
  5. binlog-ignore-db=performance_schema 
  6. binlog-ignore-db=mysql 
  7. #與主庫(kù)配置保持一致 
  8. replicate-do-db=test 
  9. replicate-ignore-db=mysql 
  10. log-slave-updates 
  11. slave-skip-errors=all 
  12. slave-net-timeout=60 

2、重啟MySQL服務(wù),登錄MySQL

  1. [root@localhost ~]# service mysqld restart 
  2.  
  3. [root@localhost ~]# mysql -u root -p 

并作如下修改:

  1. #關(guān)閉Slave 
  2. mysql> stop slave; #設(shè)置連接主庫(kù)信息 
  3. mysql> change master to master_host='192.168.0.1',master_user='root',master_password='root password',master_log_file='mysql-bin.000006', master_log_pos=120; 
  4. #開啟Slave 
  5. mysql> start slave; 

注:上面的master_log_file是在配置Master的時(shí)候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一對(duì)應(yīng)

3、查看從庫(kù)狀態(tài)信息

  1. mysql> show slave status \G; 

成功的話會(huì)顯示如下信息:

 

  1. *************************** 1. row *************************** 
  2.                Slave_IO_State: Waiting for master to send event 
  3.                   Master_Host: 192.168.0.1 
  4.                   Master_User: root 
  5.                   Master_Port: 3306 
  6.                 Connect_Retry: 60 
  7.               Master_Log_File: mysql-bin.000006 
  8.           Read_Master_Log_Pos: 120 
  9.                Relay_Log_File: localhost-relay-bin.000006 
  10.                 Relay_Log_Pos: 520 
  11.         Relay_Master_Log_File: mysql-bin.000006 
  12.              Slave_IO_Running: Yes //顯示yes為成功 
  13.             Slave_SQL_Running: Yes //顯示yes為成功,如果為no,一般為沒有啟動(dòng)master 
  14.               Replicate_Do_DB: test 
  15.           Replicate_Ignore_DB: mysql//上面的都是配置文件中的信息 
  16.            Replicate_Do_Table: 
  17.        Replicate_Ignore_Table: 
  18.       Replicate_Wild_Do_Table: 
  19.   Replicate_Wild_Ignore_Table: 
  20.                    Last_Errno: 0 
  21.                    Last_Error: 
  22.                  Skip_Counter: 0 
  23.           Exec_Master_Log_Pos: 357 
  24.               Relay_Log_Space: 697 
  25.               Until_Condition: None 
  26.                Until_Log_File: 
  27.                 Until_Log_Pos: 0 
  28.            Master_SSL_Allowed: No 
  29.            Master_SSL_CA_File: 
  30.            Master_SSL_CA_Path: 
  31.               Master_SSL_Cert: 
  32.             Master_SSL_Cipher: 
  33.                Master_SSL_Key: 
  34.         Seconds_Behind_Master: 0 
  35. Master_SSL_Verify_Server_Cert: No 
  36.                 Last_IO_Errno: 0 
  37.                 Last_IO_Error: //如果為no,此處會(huì)顯示錯(cuò)誤信息 
  38.                Last_SQL_Errno: 0 
  39.                Last_SQL_Error: 
  40.   Replicate_Ignore_Server_Ids: 
  41.              Master_Server_Id: 2 
  42.                   Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a 
  43.              Master_Info_File: /usr/local/mysql/data/master.info 
  44.                     SQL_Delay: 0 
  45.           SQL_Remaining_Delay: NULL 
  46.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 
  47.            Master_Retry_Count: 86400 
  48.                   Master_Bind: 
  49.       Last_IO_Error_Timestamp: 
  50.      Last_SQL_Error_Timestamp: 
  51.                Master_SSL_Crl: 
  52.            Master_SSL_Crlpath: 
  53.            Retrieved_Gtid_Set: 
  54.             Executed_Gtid_Set: 
  55.                 Auto_Position: 0 
  56. 1 row in set (0.00 sec) 
  57.   
  58. ERROR: 
  59. No query specified 

注:如果Slave_IO_Running: No并且出現(xiàn)下面的錯(cuò)誤

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.

說(shuō)明主服務(wù)器的UUID和從服務(wù)器的UUID重復(fù),因?yàn)槲沂前惭b成功一臺(tái)數(shù)據(jù)庫(kù)后直接克隆的,所以他們的UUID是一樣的,就會(huì)報(bào)這個(gè)錯(cuò)。可以修改一下從庫(kù)的UUID即可。

我們先在從庫(kù)的數(shù)據(jù)庫(kù)中生成一個(gè)UUID

  1. mysql>select UUID(); 

將數(shù)據(jù)庫(kù)中查詢出來(lái)的這個(gè)UUID復(fù)制出來(lái),然后編輯從庫(kù)的UUID配置文件

如果你也安裝的跟我一樣,那么這個(gè)配置文件的路徑就應(yīng)該在這里:

  1. [root@localhost ~]# vi /usr/local/mysql/data/auto.cnf 

進(jìn)去后,將一串32位長(zhǎng)的UUID,替換成我們剛在數(shù)據(jù)庫(kù)中查詢生成的UUID即可。

如果Slave_IO_Running: No 并出現(xiàn)下面錯(cuò)誤

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

解決方法:復(fù)位

  1. mysql>stop slave; //停止 
  2. mysql>reset slave; //復(fù)位 
  3. mysql>start slave; //開啟 

至此整個(gè)過(guò)程就配置好了。

可能有小伙伴會(huì)問,這些配置文件我都配好了,信息也和你的一樣,我還是不確定是否配置成功。

那么你可以在主服務(wù)器上創(chuàng)建一個(gè)表,然后在從服務(wù)器上查詢剛創(chuàng)建的這個(gè)表,看是否存在就可以啦。

Tips

1、關(guān)于增刪改查,主從數(shù)據(jù)不一致問題:

  1. #select 語(yǔ)句,暫時(shí)沒有發(fā)現(xiàn)問題 
  2.   
  3. #insert 語(yǔ)句,暫時(shí)沒有發(fā)現(xiàn)問題 
  4.   
  5. #update 語(yǔ)句,暫時(shí)沒有發(fā)現(xiàn)問題 
  6.   
  7. #delete 語(yǔ)句,主庫(kù)刪除多條數(shù)據(jù),發(fā)現(xiàn)數(shù)據(jù)不一致 

原因:在主庫(kù)的logbin中的確有執(zhí)行刪除語(yǔ)句,但是在從庫(kù)的logbin中卻沒有刪除語(yǔ)句

解決:使用 use database 選取當(dāng)前數(shù)據(jù)庫(kù)架構(gòu)中的需要操作的數(shù)據(jù)庫(kù),然后在執(zhí)行刪除,OK同步成功

2、查詢binlog主從日志的方法

  1. #查看binlog全部文件 
  2. mysql>show binary logs; 
  3.   
  4. #查看binlog是否開啟NO為開啟 
  5. mysql> show variables like 'log_bin%'
  6.   
  7. #詳細(xì)信息 
  8. mysql>  show variables like 'binlog%'
  9.   
  10. #查看binlog日志 
  11. mysql> show binlog events in'mysql-bin.000019'
  12.   
  13. #或者使用mysqlbinlog,如果報(bào)錯(cuò)使用--no-defaults(使用全路徑) 
  14. [root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019 

3、手動(dòng)清理master日志,最好關(guān)閉日志,在/etc/my.cnf

  1. #手動(dòng)刷新日志 
  2. mysql> show master status; 
  3. #刪除全部 
  4. mysql> reset slave;或 rest master; 
  5. #刪除MySQL-bin.004 
  6. mysql> PURGE MASTER LOGS TO 'MySQL-bin.004'

此外,如果你在修改最大連接數(shù)時(shí),可能會(huì)存在已經(jīng)將mysql配置文件的連接數(shù)改成1000或更大,但是查詢數(shù)據(jù)庫(kù)的最大連接數(shù)始終都是214,可以嘗試如下方法:

https://www.cnblogs.com/brucetang/p/9733998.html

參考

https://javawind.net/p141

 

https://www.cnblogs.com/brucetang/p/9733998.html

 

責(zé)任編輯:武曉燕 來(lái)源: SQL數(shù)據(jù)庫(kù)開發(fā)
相關(guān)推薦

2024-03-01 18:33:59

MySQL節(jié)點(diǎn)數(shù)據(jù)

2024-07-04 08:00:24

2023-03-08 08:44:47

2025-02-10 10:55:16

2018-04-08 15:20:15

數(shù)據(jù)庫(kù)MySQL主從復(fù)制

2023-03-19 22:38:12

邏輯復(fù)制PostgreSQL

2023-03-19 11:53:27

2025-01-15 15:47:36

2022-12-20 08:46:41

MySQL主從復(fù)制

2023-07-03 08:57:45

Master服務(wù)TCP

2023-09-24 14:32:15

2011-04-06 09:59:00

MySQL數(shù)據(jù)庫(kù)主從復(fù)制

2017-10-11 15:40:20

MySQL主從復(fù)制拓?fù)浣Y(jié)構(gòu)

2017-09-05 16:00:49

MySQL主從復(fù)制備份

2023-02-27 07:33:14

MySQL數(shù)據(jù)庫(kù)服務(wù)器

2021-03-19 11:33:42

MySQL數(shù)據(jù)庫(kù)備份

2021-01-12 09:03:17

MySQL復(fù)制半同步

2024-07-04 17:22:23

2017-06-23 22:00:13

MySqlsslcentos

2020-04-14 16:26:22

MySQL線程同步
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)