日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql多源复制 知乎_MySQL多主一从(多源复制)同步配置

發(fā)布時(shí)間:2023/12/10 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql多源复制 知乎_MySQL多主一从(多源复制)同步配置 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

>多主一從,也稱為多源復(fù)制,數(shù)據(jù)流向。這是小編的服務(wù)器部署的一次小嘗試,實(shí)際工作中還沒(méi)遇到過(guò)

形式

主庫(kù)1 -> 從庫(kù)s

主庫(kù)2 -> 從庫(kù)s

主庫(kù)n -> 從庫(kù)s

應(yīng)用場(chǎng)景

數(shù)據(jù)匯總,可將多個(gè)主數(shù)據(jù)庫(kù)同步匯總到一個(gè)從數(shù)據(jù)庫(kù)中,方便數(shù)據(jù)統(tǒng)計(jì)分析。

讀寫(xiě)分離,從庫(kù)只用于查詢,提高數(shù)據(jù)庫(kù)整體性能。

部署環(huán)境

注:使用docker部署mysql實(shí)例,方便快速搭建演示環(huán)境。但本文重點(diǎn)是講解主從配置,因此簡(jiǎn)略描述docker環(huán)境構(gòu)建mysql容器實(shí)例。(亦或者可以使用一鍵安裝環(huán)境的插件,在這里不做詳說(shuō))若不熟悉,可使用傳統(tǒng)方式安裝mysql,效果相同。

數(shù)據(jù)庫(kù):MySQL 5.7.x (相比5.5,5.6而言,5.7同步性能更好,支持多源復(fù)制,可實(shí)現(xiàn)多主一從,主從庫(kù)版本應(yīng)保證一致)

操作系統(tǒng):CentOS 7.x

容器:Docker 17.09.0-ce

鏡像:mysql:5.7

主庫(kù)300:IP=192.168.1.218; PORT=3306; server-id=2; database=test3; table=user

主庫(kù)200:IP=192.168.1.225; PORT=3306; server-id=1; database=test4; table=user

從庫(kù):IP=192.168.1.128; PORT=3306; server-id=3; database=test3,test4; table=user

配置約束

主從庫(kù)必須保證網(wǎng)絡(luò)暢通可訪問(wèn)

主庫(kù)必須開(kāi)啟binlog日志

主從庫(kù)的server-id必須不同

【主庫(kù)300】操作及配置

配置my.cnf

[client]

#password = your_passwordport = 3306

socket = /tmp/mysql.sock

[mysqld]

port = 3306 #默認(rèn)端口socket = /tmp/mysql.sock

datadir = /usr/local/mysql/var

skip-external-locking

key_buffer_size = 32M

max_allowed_packet = 1M

table_open_cache = 128

sort_buffer_size = 768K

net_buffer_length = 8K

read_buffer_size = 768K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

thread_cache_size = 16

query_cache_size = 16M

tmp_table_size = 32M

performance_schema_max_table_instances = 1000

skip-grant-tables

explicit_defaults_for_timestamp = true

#skip-networkingmax_connections = 500

max_connect_errors = 100

open_files_limit = 65535

log-bin=mysql-bin #開(kāi)啟二進(jìn)制日志記錄binlog_format=mixed

server-id = 3 #配置唯一idexpire_logs_days = 10 #日志過(guò)期時(shí)間(天)early-plugin-load = ""

#不給從機(jī)同步的庫(kù)binlog-ignore-db=sys

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

default_storage_engine = InnoDB

innodb_file_per_table = 1

innodb_data_home_dir = /usr/local/mysql/var

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mysql/var

innodb_buffer_pool_size = 128M

innodb_log_file_size = 32M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

創(chuàng)建授權(quán)用戶

mysql重啟,連接mysql主數(shù)據(jù)庫(kù),鍵入命令mysql -u root -p,輸入密碼后登錄數(shù)據(jù)庫(kù)。創(chuàng)建用戶用于從庫(kù)同步復(fù)制,授予復(fù)制、同步訪問(wèn)的權(quán)限;

mysql> CREATE USER '用戶名'@'%' IDENTIFIED BY '密碼';#創(chuàng)建用戶mysql> GRANT REPLICATION SLAVE ON *.* TO '用戶名'@'%';#分配權(quán)限mysql>flush privileges; #刷新權(quán)限

查看log_bin是否開(kāi)啟

mysql> show variables like 'log_bin';

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

| Variable_name | Value |

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

| log_bin | ON |

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

1 row in set (0.00 sec)

查看master狀態(tài),記錄二進(jìn)制文件名(mysql-bin.000006)和位置(916)

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000006 | 916 | | sys,mysql,information_schema,performance_schema | |

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

1 row in set (0.00 sec)

【主庫(kù)400】配置及操作

配置my.cnf

[client]

#password = your_passwordport = 3306

socket = /tmp/mysql.sock

[mysqld]

port = 3306

socket = /tmp/mysql.sock

datadir = /usr/local/mysql/var

skip-external-locking

key_buffer_size = 32M

max_allowed_packet = 1M

table_open_cache = 128

sort_buffer_size = 768K

net_buffer_length = 8K

read_buffer_size = 768K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

thread_cache_size = 16

query_cache_size = 16M

tmp_table_size = 32M

performance_schema_max_table_instances = 1000

explicit_defaults_for_timestamp = true

#skip-networkingmax_connections = 500

max_connect_errors = 100

open_files_limit = 65535

log-bin=mysql-bin #開(kāi)啟二進(jìn)制日志binlog_format=mixed

server-id = 1 #唯一idexpire_logs_days = 10 #過(guò)期日期(天)early-plugin-load = ""

#不給從機(jī)同步的庫(kù)binlog-ignore-db=sys

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

default_storage_engine = InnoDB

innodb_file_per_table = 1

innodb_data_home_dir = /usr/local/mysql/var

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mysql/var

innodb_buffer_pool_size = 128M

innodb_log_file_size = 32M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump]

quick

"/etc/my.cnf" 69L, 1430C

創(chuàng)建授權(quán)用戶

mysql重啟,連接mysql主數(shù)據(jù)庫(kù),鍵入命令mysql -u root -p,輸入密碼后登錄數(shù)據(jù)庫(kù)。創(chuàng)建用戶用于從庫(kù)同步復(fù)制,授予復(fù)制、同步訪問(wèn)的權(quán)限;

mysql> CREATE USER '用戶名'@'%' IDENTIFIED BY '密碼';#創(chuàng)建用戶mysql> GRANT REPLICATION SLAVE ON *.* TO '用戶名'@'%';#分配權(quán)限mysql>flush privileges; #刷新權(quán)限

注:各個(gè)主機(jī)創(chuàng)建的用戶可以相同的。

查看log_bin是否開(kāi)啟

mysql> show variables like 'log_bin';

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

| Variable_name | Value |

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

| log_bin | ON |

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

1 row in set (0.00 sec)

查看master狀態(tài),記錄二進(jìn)制文件名(mysql-bin.000008)和位置(892)

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000008 | 892 | | sys,mysql,information_schema,performance_schema | |

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

1 row in set (0.00 sec)

【從庫(kù)】配置及操作

配置my.cnf

[client]

#password = your_passwordport = 3306

socket = /tmp/mysql.sock

[mysqld]

port = 3306

socket = /tmp/mysql.sock

datadir = /usr/local/mysql/var

skip-external-locking

key_buffer_size = 32M

max_allowed_packet = 1M

table_open_cache = 128

sort_buffer_size = 768K

net_buffer_length = 8K

read_buffer_size = 768K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

thread_cache_size = 16

query_cache_size = 16M

tmp_table_size = 32M

performance_schema_max_table_instances = 1000

explicit_defaults_for_timestamp = true

#skip-networkingmax_connections = 500

max_connect_errors = 100

open_files_limit = 65535

#log-bin=mysql-bin #關(guān)閉二進(jìn)制日志binlog_format=mixed

server-id = 2 #配置唯一idexpire_logs_days = 10 #過(guò)期時(shí)間(天)early-plugin-load = ""

#加上以下參數(shù)避免更新不及時(shí),slave 重啟后導(dǎo)致的主從復(fù)制出錯(cuò)read_only = 1

master_info_repository=TABLE

relay_log_info_repository=TABLE

relay_log_recovery=1 #從機(jī)禁止寫(xiě)操作

default_storage_engine = InnoDB

innodb_file_per_table = 1

innodb_data_home_dir = /usr/local/mysql/var

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mysql/var

innodb_buffer_pool_size = 128M

innodb_log_file_size = 32M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump]

quick

"/etc/my.cnf" 69L, 1494C

重啟mysql,打開(kāi)mysql會(huì)話,執(zhí)行同步SQL語(yǔ)句(需要主服務(wù)器主機(jī)名,登陸憑據(jù),二進(jìn)制文件的名稱和位置):

php mysql> CHANGE MASTER TO

-> MASTER_HOST='192.168.1.218',

-> MASTER_USER='用戶名',

-> MASTER_PASSWORD='密碼',

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

-> MASTER_LOG_POS=916

-> for channel '300';

mysql> CHANGE MASTER TO

-> MASTER_HOST='192.168.1.225',

-> MASTER_USER='用戶名',

-> MASTER_PASSWORD='密碼',

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

-> MASTER_LOG_POS=892

-> for channel '200';

關(guān)鍵點(diǎn)解說(shuō)

stop slave; //停止同步

start slave; //開(kāi)始同步

//必須和【主庫(kù)】的信息匹配。

CHANGE MASTER TO MASTER_HOST='192.168.10.212', //主庫(kù)IP

MASTER_PORT=4300, //主庫(kù)端口

MASTER_USER='slave', //訪問(wèn)主庫(kù)且有同步復(fù)制權(quán)限的用戶

MASTER_PASSWORD='123456', //登錄密碼//【關(guān)鍵處】從主庫(kù)的該log_bin文件開(kāi)始讀取同步信息,主庫(kù)show master status返回結(jié)果

MASTER_LOG_FILE='mysql-bin.000003',//【關(guān)鍵處】從文件中指定位置開(kāi)始讀取,主庫(kù)show master status返回結(jié)果

MASTER_LOG_POS=438

for channel '300'; //定義通道名稱

啟動(dòng)slave同步進(jìn)程

mysql>start slave;

查看同步狀態(tài)

mysql> show slave status \G;

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.225

Master_User: root2

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000008

Read_Master_Log_Pos: 892

Relay_Log_File: localhost-relay-bin-200.000061

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000008

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

Relay_Log_Space: 701

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

Master_UUID: 6c83a613-5cfe-11e9-92a0-000c29804965

Master_Info_File: mysql.slave_master_info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

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

Replicate_Rewrite_DB:

Channel_Name: 200

Master_TLS_Version:

*************************** 2. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.218

Master_User: root3

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000006

Read_Master_Log_Pos: 916

Relay_Log_File: localhost-relay-bin-300.000065

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000006

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

Relay_Log_Space: 701

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

Master_UUID: 164794cb-7557-11e9-bb7c-000c29b01621

Master_Info_File: mysql.slave_master_info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

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

Replicate_Rewrite_DB:

Channel_Name: 300

Master_TLS_Version:

2 rows in set (0.00 sec)

ERROR:

No query specified

可以看見(jiàn)設(shè)置兩個(gè)的主從同步通道的所有狀態(tài)信息。只有【Slave_IO_Running】和【Slave_SQL_Running】都是Yes,則同步是正常的。 如果是No或者Connecting都不行,一般出錯(cuò)都是【MASTER_LOG_FILE】和【MASTER_LOG_POS】不一致問(wèn)題,需要細(xì)細(xì)核查才行。

配置完成,則【從庫(kù)】開(kāi)始自動(dòng)同步。若需要單獨(dú)啟動(dòng)或停止某個(gè)同步通道,可使用如下命令:

start slave for channel '300'; //啟動(dòng)名稱為300的同步通道stop slave for channel '300'; //停止名稱為300的同步通道

同步功能可自行驗(yàn)證(親測(cè)有效)

參考

MySQL5.7多主一從(多源復(fù)制)同步配置

MySQL主從復(fù)制(Master-Slave)實(shí)踐???????

總結(jié)

以上是生活随笔為你收集整理的mysql多源复制 知乎_MySQL多主一从(多源复制)同步配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。