{"id":101081,"date":"2024-08-09T11:04:10","date_gmt":"2024-08-09T03:04:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=101081"},"modified":"2024-08-09T11:04:10","modified_gmt":"2024-08-09T03:04:10","slug":"mariadb-master-savle","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mariadb-master-savle.html","title":{"rendered":"MariaDB\u6570\u636e\u5e93\u57fa\u4e8eGTID\u4e3b\u4ece\u540c\u6b65\u590d\u5236\u5b89\u88c5\u914d\u7f6e"},"content":{"rendered":"
\u64cd\u4f5c\u7cfb\u7edf:CentOS Linux release 7.3.1611 (Core)<\/p>\n
\u6570\u636e\u5e93:MariaDB-10.2.6-linux-glibc_214-x86_64<\/p>\n
\u4e3b\u670d\u52a1\u5668:10.10.10.56<\/p>\n
\u4ece\u670d\u52a1\u5668:10.10.10.163<\/p>\n
<\/p>\n
1.\u4eca\u5929\u8981\u505a\u7684\u4e8b\uff0c\u5355\u5411\u4e3b\u4ece\u540c\u6b65<\/p>\n
\u5173\u4e8eMariaDB(MySQL)\u6570\u636e\u5e93\u5b89\u88c5\u8bf7\u53c2\u9605\u300aCentos7.3 x64\u4f4d\u4e8c\u8fdb\u5236\u5b89\u88c5\uff08MySQL\uff09MariaDB 10.1.20\u6570\u636e\u5e93\u4e4b\u751f\u4ea7\u7bc7\u300b\u4e0b\u9762\u5c31\u5f00\u59cbMariaDB(MySQL)\u670d\u52a1\u5668\u96c6\u7fa4\u7684\u914d\u7f6e\u8fc7\u7a0b\u3002<\/p>\n
2.\u5728MariaDB(MySQL)\u914d\u7f6e\u6587\u4ef6\u4e2d\u4fee\u6539\u6216\u6dfb\u52a0\u4ee5\u4e0b\u4fe1\u606f<\/p>\n
\r\nvim \/etc\/my.cnf\r\n\r\n\u4e3b\u4ece\u901a\u7528\u914d\u7f6e\r\n\r\nbinlog-format = mixed #\u4e8c\u8fdb\u5236\u65e5\u5fd7\u8bb0\u5f55\u7684\u6a21\u5f0f\uff08\u9ad8\u7248\u672c\u9ed8\u8ba4\u5f00\u542f\uff09\r\nbinlog-checksum = CRC32 #\u53ef\u4f7f\u4e3b\u673a\u4e3a\u5199\u5165\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u4e8b\u4ef6\u5199\u5165\u6821\u9a8c\uff08\u9ad8\u7248\u672c\u9ed8\u8ba4\u5f00\u542f\uff09\r\nsync-master-info = 1 #MariaDB\u4f9d\u9760\u64cd\u4f5c\u7cfb\u7edf\u5c06master.info\u6587\u4ef6\u5237\u65b0\u5230\u78c1\u76d8\u3002\r\nsync_relay_log_info = 1 #MariaDB\u4f9d\u9760\u64cd\u4f5c\u7cfb\u7edf\u5c06relay-log.info\u6587\u4ef6\u5237\u65b0\u5230\u78c1\u76d8\u3002\r\nexpire_logs_days = 7 #\u65e5\u5fd7\u6587\u4ef6\u8fc7\u671f\u5929\u6570,\u9ed8\u8ba4\u662f 0,\u8868\u793a\u4e0d\u8fc7\u671f \r\nmaster-verify-checksum = 1 #\u4e3b\u670d\u52a1\u5668\u6548\u9a8c\r\nslave-sql-verify-checksum = 1 #\u4ece\u670d\u52a1\u5668\u6548\u9a8c\r\n<\/pre>\n3.\u4e3b\u670d\u52a1\u5668Master\u9664\u4e86\u901a\u7528\u914d\u7f6e\u5916,\u8fd8\u9700\u8981\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801<\/p>\n
\r\nserver-id = 56 #MySQL\u670d\u52a1\u5668ID,\u4e0d\u91cd\u590d\r\nlog-bin = mysql-bin #\u4e8c\u8fdb\u5236\u65e5\u5fd7\uff08\u9ed8\u8ba4\u5f00\u542f\uff09\r\nsync-binlog = 1 #\u4e3b\u670d\u52a1\u5668\u8fdb\u884c\u8bbe\u7f6e,\u7528\u4e8e\u4e8b\u52a1\u5b89\u5168\r\nlog-bin-index = mysql-bin\r\n<\/pre>\n4.\u4ece\u670d\u52a1\u5668Slave\u9664\u4e86\u901a\u7528\u914d\u7f6e\u5916,\u8fd8\u9700\u8981\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801<\/p>\n
\r\nserver-id = 163\r\nrelay-log = relay-bin #\u4e2d\u7ee7\u65e5\u5fd7\r\nslave-parallel-threads = 2 #\u8bbe\u5b9a\u4ece\u670d\u52a1\u5668\u7684SQL\u7ebf\u7a0b\u6570\r\n#replicate-do-db = renwoleblogdb#\u590d\u5236\u6307\u5b9a\u7684\u6570\u636e\u5e93,\u591a\u4e2a\u5199\u591a\u884c\r\nreplicate-ignore-db = mysql #\u4e0d\u5907\u4efd\u7684\u6570\u636e\u5e93,\u591a\u4e2a\u5199\u591a\u884c\r\nrelay_log_recovery = 1 #\u4ece\u7ad9\u5d29\u6e83\u540e\u53ef\u4ee5\u4f7f\u7528,\u9632\u6b62\u635f\u574f\u7684\u4e2d\u7ee7\u65e5\u5fd7\u5904\u7406\u3002\r\nlog-slave-updates = 1 #slave\u5c06\u590d\u5236\u4e8b\u4ef6\u5199\u8fdb\u81ea\u5df1\u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\r\nrelay-log-index = relay-bin\r\n<\/pre>\n\u6b64\u5916Mysql\u4ece\u670d\u52a1\u5668\u6ca1\u6709\u5fc5\u8981\u5f00\u542f\u4e8c\u8fdb\u5236\u65e5\u5fd7\uff0c\u4f46\u662f\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b,\u5fc5\u987b\u8bbe\u7f6e,\u4f8b\u5982;\u5982\u679cslave\u4e3a\u5176\u5b83slave\u7684master\uff0c\u5fc5\u987b\u8bbe\u7f6ebin_log\u3002\u6211\u8fd9\u91cc\u5c31\u9ed8\u8ba4\u5f00\u542f\u3002<\/p>\n
5.\u4ee5\u4e0a\u53ea\u662f\u7b80\u5355\u7684\u4ecb\u7ecd\u4e86\u6bcf\u4e2a\u53c2\u6570\u7684\u4f5c\u7528,\u8fd9\u4e9b\u53c2\u6570\u7684\u5177\u4f53\u8bbe\u7f6e\u8fd8\u9700\u6839\u636e\u7528\u6237\u7684\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u76f8\u5173\u8c03\u6574,\u5177\u4f53\u53ef\u5230\u5b98\u65b9\u4e86\u89e3<\/p>\n
\r\n\u300a\u590d\u5236\u548c\u4e8c\u8fdb\u5236\u65e5\u5fd7\u670d\u52a1\u5668\u7cfb\u7edf\u53d8\u91cf\u300b\r\nhttps:\/\/mariadb.com\/kb\/en\/mariadb\/replication-and-binary-log-server-system-variables\/\r\n\r\n\u5173\u4e8e\u7cfb\u7edf\u53d8\u91cf\u7684\u517c\u5bb9\u6027\uff0c\u53ef\u53c2\u9605\u5b98\u65b9\r\n\r\n\u300aMariaDB\u4e0eMySQL\u517c\u5bb9\u6027\u300b\r\nhttps:\/\/mariadb.com\/kb\/en\/mariadb\/mariadb-vs-mysql-compatibility\/\r\n<\/pre>\n6.\u4e3b\u670d\u52a1\u5668Master\u6388\u6743\u914d\u7f6e<\/p>\n
\u4e3bMariaDB\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e13\u7528\u8d26\u53f7\u5e76\u6388\u6743\u6570\u636e\u5e93\u6743\u9650,\u4ee5\u53ca\u4ece\u670d\u52a1\u5668IP\u7684\u8fdc\u7a0b\u8bbf\u95ee<\/p>\n
\r\n# mysql -uroot -p\r\nEnter password:\u3010\u8f93\u5165\u4f60\u7684MySQL\u5bc6\u7801\u56de\u8f66\u3011\r\nMariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'renwoleuseracc'@'%' IDENTIFIED BY 'renwoleuserpass'; \/\/\u521b\u5efaSlave\u4e13\u7528\u5907\u4efd\u8d26\u53f7\r\nMariaDB [(none)]> flush privileges; \/\/\u5237\u65b0MySQL\u6743\u9650\r\nMariaDB [(none)]> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; \/\/\u67e5\u770b\u6388\u6743\u60c5\u51b5\r\nMariaDB [(none)]> flush tables with read lock; \/\/\u9501\u5b9a\u6570\u636e\u5e93\u9632\u6b62master\u503c\u53d8\u5316\r\nMariaDB [(none)]> show master status; \/\/\u83b7\u53d6master\u72b6\u6001\u503c\r\n+-----------------+----------+------------+-----------------+\r\n| File |Position |Binlog_Do_DB|Binlog_Ignore_DB |\r\n+-----------------+----------+------------+-----------------+\r\n| mysql-bin.000006| 627 | | |\r\n+-----------------+----------+------------+-----------------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n7.\u4e00\u65e6\u83b7\u53d6\u4e86\u5907\u4efd\u65f6\u6b63\u786e\u7684Binlog\u4f4d\u70b9\uff08\u6587\u4ef6\u540d\u548c\u504f\u79fb\u91cf\uff09\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u7528BINLOG_GTID_POS()\u51fd\u6570\u6765\u8ba1\u7b97GTID<\/p>\n
\r\nMariaDB [(none)]> SELECT BINLOG_GTID_POS(\"mysql-bin.000006\", 627);\r\n+------------------------------------------+\r\n| BINLOG_GTID_POS('mysql-bin.000006', 627) |\r\n+------------------------------------------+\r\n| 0-56-4 |\r\n+------------------------------------------+\r\n1 row in set (0.01 sec)\r\n<\/pre>\n8.\u4ece\u670d\u52a1\u5668Slave\u914d\u7f6e<\/p>\n
\u6b63\u5982\u5b98\u65b9\u6240\u8bf4\u4eceMariaDB 10.0.13\u7248\u672c\u5f00\u59cb\uff0cmysqldump\u4f1a\u81ea\u52a8\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\uff0c\u5e76\u4e14\u628aGTID\u7684\u5199\u5728\u5bfc\u51fa\u6587\u4ef6\u4e2d\uff0c\u53ea\u8981\u8bbe\u7f6e \u2013master-data \u6216 \u2013dump-slave \u7684\u540c\u65f6\u8bbe\u7f6e \u2013gtid \u5373\u53ef\u3002<\/p>\n
\u8fd9\u6837\u7684\u8bdd\u65b0\u7684SLAVE\u5c31\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e @@gtid_slave_pos \u7684\u503c\u6765\u8bbe\u5b9a\u590d\u5236\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u7528 CHANGE MASTER \u628a\u8fd9\u4e2a\u503c\u4f20\u7ed9\u4e3b\u5e93\uff0c\u7136\u540e\u5f00\u59cb\u590d\u5236\uff1a<\/p>\n
\r\n# mysql -uroot -p\r\nEnter password:\u3010\u8f93\u5165\u4f60\u7684MySQL\u5bc6\u7801\u3011\r\nMariaDB [(none)]> SET GLOBAL gtid_slave_pos = \"0-56-4\";\r\nMariaDB [(none)]> change master to master_host='10.10.10.56',MASTER_PORT = 3306,master_user='renwoleuseracc',master_password='renwoleuserpass',master_use_gtid=slave_pos; \/\/\u8fdb\u884c\u4e3b\u4ece\u6388\u6743\r\nMariaDB [(none)]> START SLAVE; \/\/\u542f\u52a8Slave\r\nMariaDB [(none)]> show slave status\\G\r\n*************************** 1. row ***************************\r\n Slave_IO_State: Waiting for master to send event\r\n Master_Host: 10.10.10.56\r\n Master_User: renwoleuseracc\r\n Master_Port: 3306\r\n Connect_Retry: 60\r\n Master_Log_File: mysql-bin.000006\r\n Read_Master_Log_Pos: 627\r\n Relay_Log_File: relay.000035\r\n Relay_Log_Pos: 537\r\n Relay_Master_Log_File: mysql-bin.000006\r\n Slave_IO_Running: Yes\r\n Slave_SQL_Running: Yes\r\n ...\r\n ...\r\n ...\r\n Using_Gtid: Slave_pos\r\n Gtid_IO_Pos: 0-56-4\r\n<\/pre>\n9.\u5982\u679c Slave_IO_Running \u4e0e Slave_SQL_Running \u90fd\u4e3aYES,\u8868\u660e\u4ece\u670d\u52a1\u5df2\u7ecf\u8fd0\u884c\uff0cUsing_Gtid\u5217\u5224\u65adGTID\u503c\u662f\u5426\u4e00\u81f4\u3002
\n\u8bf4\u660e\uff1a<\/p>\n\r\nmaster_host \u8868\u793amaster\u6388\u6743\u5730\u5740\r\nMASTER_PORT MySQL\u7aef\u53e3\r\nmaster_user \u8868\u793amaster\u6388\u6743\u8d26\u53f7\r\nmaster_password \u8868\u793a\u5bc6\u7801\r\nmaster_use_gtid GTID\u53d8\u91cf\u503c\r\n<\/pre>\n10.\u63a5\u4e0b\u6765\u89e3\u9501\u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u8868<\/p>\n
\r\nMariaDB [(none)]> unlock tables; \/\/\u89e3\u9501\u6570\u636e\u8868\r\nMariaDB [(none)]> show slave hosts; \/\/\u67e5\u770b\u4ece\u670d\u52a1\u5668\u8fde\u63a5\u72b6\u6001\r\nMariaDB [(none)]> show global status like \"rpl%\"; \/\/\u67e5\u770b\u5ba2\u6237\u7aef\r\n<\/pre>\n11.\u4ece\u670d\u52a1\u5668Slave\u67e5\u770brelay\u7684\u6240\u6709\u76f8\u5173\u53c2\u6570<\/p>\n
\r\nMariaDB [(none)]> show variables like '%relay%';\r\n<\/pre>\n12.\u4e3b\u4ece\u5df2\u7ecf\u914d\u7f6e\u5b8c\u6210\u3002\u73b0\u5728\u65e0\u8bba\u5728\u4e3b\u670d\u52a1\u5668\u4e0a\u589e\u3001\u6539\u3001\u5220\u3001\u67e5\uff0c\u90fd\u4f1a\u540c\u6b65\u5230\u4ece\u670d\u52a1\u5668\uff0c\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u76f8\u5173\u6d4b\u8bd5\u5373\u53ef\u3002
\n\u5173\u4e8emaster slave\u91cd\u7f6e\u8bed\u6cd5<\/p>\n\u91cd\u7f6emaster\u7684\u6838\u5fc3\u8bed\u6cd5<\/p>\n
RESET MASTER; \u610f\u601d\u662f\u6267\u884c RESET MASTER \u5c06\u5220\u9664\u6240\u6709\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6\uff0c\u6570\u5b57\u540e\u7f00\u4e3a.000001\uff0cRESET MASTER \u4e0d\u4f1a\u5f71\u54cdSLAVE\u670d\u52a1\u5668\u4e0a\u7684\u5de5\u4f5c\u72b6\u6001\uff0c\u6240\u4ee5\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5bfc\u81f4Slave\u627e\u4e0d\u5230Master\u7684binlog\uff0c\u4ece\u800c\u9020\u6210\u540c\u6b65\u5931\u8d25\u3002<\/p>\n
\u91cd\u7f6eslave\u7684\u6838\u5fc3\u8bed\u6cd5<\/p>\n
RESET SLAVE; \u542b\u4e49\u662f\uff1bRESET SLAVE \u5c06\u6e05\u9664slave\u4e0a\u7684\u540c\u6b65\u4f4d\u7f6e\u5e76\u5220\u9664\u6240\u6709\u65e7\u7684\u540c\u6b65\u4e2d\u7ee7\u65e5\u5fd7\u6587\u4ef6\uff0c\u4f46\u91cd\u7f6e\u524d\u5fc5\u987b\u5148\u505c\u6b62slave\u670d\u52a1\uff08STOP SLAVE\uff09<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/renwole.com\/archives\/208<\/a><\/p>\n