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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

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

>多主一從,也稱為多源復制,數據流向。這是小編的服務器部署的一次小嘗試,實際工作中還沒遇到過

形式

主庫1 -> 從庫s

主庫2 -> 從庫s

主庫n -> 從庫s

應用場景

數據匯總,可將多個主數據庫同步匯總到一個從數據庫中,方便數據統計分析。

讀寫分離,從庫只用于查詢,提高數據庫整體性能。

部署環境

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

數據庫:MySQL 5.7.x (相比5.5,5.6而言,5.7同步性能更好,支持多源復制,可實現多主一從,主從庫版本應保證一致)

操作系統:CentOS 7.x

容器:Docker 17.09.0-ce

鏡像:mysql:5.7

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

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

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

配置約束

主從庫必須保證網絡暢通可訪問

主庫必須開啟binlog日志

主從庫的server-id必須不同

【主庫300】操作及配置

配置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

skip-grant-tables

explicit_defaults_for_timestamp = true

#skip-networkingmax_connections = 500

max_connect_errors = 100

open_files_limit = 65535

log-bin=mysql-bin #開啟二進制日志記錄binlog_format=mixed

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

#不給從機同步的庫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

創建授權用戶

mysql重啟,連接mysql主數據庫,鍵入命令mysql -u root -p,輸入密碼后登錄數據庫。創建用戶用于從庫同步復制,授予復制、同步訪問的權限;

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

查看log_bin是否開啟

mysql> show variables like 'log_bin';

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

| Variable_name | Value |

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

| log_bin | ON |

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

1 row in set (0.00 sec)

查看master狀態,記錄二進制文件名(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)

【主庫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 #開啟二進制日志binlog_format=mixed

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

#不給從機同步的庫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

創建授權用戶

mysql重啟,連接mysql主數據庫,鍵入命令mysql -u root -p,輸入密碼后登錄數據庫。創建用戶用于從庫同步復制,授予復制、同步訪問的權限;

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

注:各個主機創建的用戶可以相同的。

查看log_bin是否開啟

mysql> show variables like 'log_bin';

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

| Variable_name | Value |

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

| log_bin | ON |

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

1 row in set (0.00 sec)

查看master狀態,記錄二進制文件名(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)

【從庫】配置及操作

配置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 #關閉二進制日志binlog_format=mixed

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

#加上以下參數避免更新不及時,slave 重啟后導致的主從復制出錯read_only = 1

master_info_repository=TABLE

relay_log_info_repository=TABLE

relay_log_recovery=1 #從機禁止寫操作

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,打開mysql會話,執行同步SQL語句(需要主服務器主機名,登陸憑據,二進制文件的名稱和位置):

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';

關鍵點解說

stop slave; //停止同步

start slave; //開始同步

//必須和【主庫】的信息匹配。

CHANGE MASTER TO MASTER_HOST='192.168.10.212', //主庫IP

MASTER_PORT=4300, //主庫端口

MASTER_USER='slave', //訪問主庫且有同步復制權限的用戶

MASTER_PASSWORD='123456', //登錄密碼//【關鍵處】從主庫的該log_bin文件開始讀取同步信息,主庫show master status返回結果

MASTER_LOG_FILE='mysql-bin.000003',//【關鍵處】從文件中指定位置開始讀取,主庫show master status返回結果

MASTER_LOG_POS=438

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

啟動slave同步進程

mysql>start slave;

查看同步狀態

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

可以看見設置兩個的主從同步通道的所有狀態信息。只有【Slave_IO_Running】和【Slave_SQL_Running】都是Yes,則同步是正常的。 如果是No或者Connecting都不行,一般出錯都是【MASTER_LOG_FILE】和【MASTER_LOG_POS】不一致問題,需要細細核查才行。

配置完成,則【從庫】開始自動同步。若需要單獨啟動或停止某個同步通道,可使用如下命令:

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

同步功能可自行驗證(親測有效)

參考

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

MySQL主從復制(Master-Slave)實踐???????

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。