galera cluster for MySQL multi-master replication_金昌数据恢

日期:2014-07-13 / 人气: / 来源:网络

galera cluster for MySQL multi-master replicationgalera cluster for MySQL multi-master replication

最近公司�I了�滋�C架伺服器�硖�理 HTTP 跟 DB Load balancer,要做到 DB 的分散式架��,首先需要同步多台�C器�Y料,也就是��入或更�尤我庖惶�喂P�Y料,另外平行的�C器也��同�r�M行更新。同步的好�可以用�碜�湓�及分散�理�B�,而要做到此功能,可子�⒖季W路上�u�r不�e的 Galera Cluster for MySQL 方案。本篇��介�B在 Ubuntu 或 CentOS 6.x final 版本如何安�b Galera 伺服器套件及�O定。要架�O Galera Cluster Server,有�煞N套件�x�瘢�一��是 Percona XtraDB Cluster 另一��是 MariaDB Galera Cluster,�@次作者��介�B後者的安�b。

Galera Cluster 介�B

�槭颤N要�x�� Galera Cluster Server,它有什�N���c及功能呢?MySQL/Galera 是一套可以同步多台 MySQL/InnoDB �C器的�布�系�y,底下可以列出功能。

同步�}�u�Y料

可�x取和��入�布�系�y�热我还��c

自���y��c�e�`,如果有��c���C,�t�布�系�y自�右瞥����c

可任意�U充��c

�裼� row level 方式�砥叫醒}�u�Y料

�纳厦婀δ芸�恚�我��可以平行任意�U充��c,��B增加伺服器到�布�系�y,要做到上面功能,就是利用 Galera library �碜龅酵�步�Y料�理,同步的����,可以�⒖� Galera library �B�Y。�@�就不再多描述了。

安�b Galera Cluster Server

本篇介�B的 MySQL Server 是使用 MariaDB 套件,而不是安�b原始的 MySQL。CentOS 和 Ubuntu 安�b方式雷同,前者是用 yum 後者�t是 aptitude,在安�b前�先下�d���的 repository �O定�n。

CentOS

$ yum -y update && yum -y upgrade
$ yum -y install MariaDB-Galera-server MariaDB-client galera

Ubuntu

$ aptitude -y update
$ aptitude -y install mariadb-galera-server-5.5 galera

��� MySQL

$ /etc/init.d/mysql start

�O定 Galera Cluster Server

先�v一下�h境,目前�共�商� Galera Server,IP 分�e是:

Node_1: 192.168.1.100
Node_2: 192.168.1.101

建立 Node_1, Node_2 MySQL User,用�碚J�C使用,先�M入 MySQL Console.

$ mysql -u root -p
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 154
Server version: 10.0.1-MariaDB-mariadb1~precise-log mariadb.org binary distribution

Copyright (c) 2000, 2012, Oracle, Monty Program Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>

建立 cluster 使用者,密�a�� 1234,��� % 跟 localhost 同�r建立。

MariaDB [(none)]> GRANT USAGE ON *.* to cluster@'%' IDENTIFIED BY '1234';
MariaDB [(none)]> GRANT ALL PRIVILEGES on *.* to cluster@'%';
MariaDB [(none)]> GRANT USAGE ON *.* to cluster@'localhost' IDENTIFIED BY '1234';
MariaDB [(none)]> GRANT ALL PRIVILEGES on *.* to cluster@'localhost';

在 192.168.1.100 建立 Galera �O定�n /etc/mysq/conf.d/wsrep.cnf

[MYSQLD] wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address="gcomm://"             wsrep_sst_auth=cluster:1234

重新��� mysqld ��看到多 listen 4567 port

tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1118/mysqld
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      1118/mysqld

在 192.168.1.101 建立 Galera �O定�n /etc/mysq/conf.d/wsrep.cnf �K且�� cluster address �O定�� 192.168.1.100

[MYSQLD] wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.1.100:4567"       wsrep_sst_auth=cluster:1234

�O定完成後,重新��� mysql

$ /etc/init.d/mysql restart

要怎�N��C�O定成功呢?可以透�^ mysql console �泶_�J

$ mysql -e "SHOW STATUS LIKE 'wsrep%';"

可以看到�Y果如下

---------------------------- --------------------------------------------------------------
| wsrep_local_state_uuid     | b4e876e0-8c1e-11e2-0800-8da732edfe2a       |
| wsrep_protocol_version     | 4      |
| wsrep_last_committed       | 516    |
| wsrep_replicated           | 0      |
| wsrep_replicated_bytes     | 0      |
| wsrep_received             | 3                        |
| wsrep_received_bytes       | 282    |
| wsrep_local_commits        | 0      |
| wsrep_local_cert_failures  | 0      |
| wsrep_local_bf_aborts      | 0      |
| wsrep_local_replays        | 0      |
| wsrep_local_send_queue     | 0      |
| wsrep_local_send_queue_avg | 0.000000                 |
| wsrep_local_recv_queue     | 0      |
| wsrep_local_recv_queue_avg | 0.000000                 |
| wsrep_flow_control_paused  | 0.000000 |
| wsrep_flow_control_sent    | 0      |
| wsrep_flow_control_recv    | 0      |
| wsrep_cert_deps_distance   | 0.000000                 |
| wsrep_apply_oooe           | 0.000000                 |
| wsrep_apply_oool           | 0.000000                 |
| wsrep_apply_window         | 0.000000 |
| wsrep_commit_oooe          | 0.000000                 |
| wsrep_commit_oool          | 0.000000                 |
| wsrep_commit_window        | 0.000000 |
| wsrep_local_state          | 4      |
| wsrep_local_state_comment  | Synced                   |
| wsrep_cert_index_size      | 0      |
| wsrep_causal_reads         | 0      |
| wsrep_incoming_addresses   | xxx.xxx.xxx.xxx:3306,xxx.xxx.xx.xxx:3306,xxx.xxx.xxx.xx:3306 |
| wsrep_cluster_conf_id      | 45     |
| wsrep_cluster_size         | 3      |
| wsrep_cluster_state_uuid   | b4e876e0-8c1e-11e2-0800-8da732edfe2a       |
| wsrep_cluster_status       | Primary                  |
| wsrep_connected            | ON     |
| wsrep_local_index          | 0      |
| wsrep_provider_name        | Galera                   |
| wsrep_provider_vendor      | Codership Oy <info@codership.com>          |
| wsrep_provider_version     | 23.2.4(r147)             |
| wsrep_ready                | ON     |
---------------------------- --------------------------------------------------------------

看到上述�Y果,有一��非常重要的�抵担�那就是 wsrep_ready,正�_值是 ON,另外看看 wsrep_cluster_size 是否跟您�O置 Node 的�盗肯嗤�,�@���如果都正�_,那就表示�O定成功了,由於上面 192.168.1.100 是主 Cluster Server,�F在我��必�互相�O定�p方 Address 也就是�O定如下

node 01: gcomm://192.168.1.101
node 02: gcomm://192.168.1.100

�O定如上述的好�就是,�� Node 01 �P�C�r,�Y料�是在 Node 2 �^�m�作,等到 Node 01 恢�蜕暇�後,�Y料又���� Node 02 同步�}�u�^�怼�

增加新 Node

我��可以任意新增多台 Node 到 Cluster �布�系�y�e,�O置�^程非常�易

1. 安�b MariaDB Server
2. 安�b Galera Library
3. �O定 wsrep_cluster_address="gcomm://192.168.1.100:4567"

�O定完成,就可以看到�Y料�煲呀�同步�}�u�Y料到新 Node 上面。如果遇到任何一台 Node 突然�P�C,不用�o��,系�y��保持目前的�Y料,等到�C器上��r,又���� gcomm://another_node_ipaddress 同步後�m的�Y料。

��B�O定 gcomm://

如果新增一台新的 Node,我��就必�改�悠渌� Node 的 gcomm:// �O定,�K且重新��� mysqld 服�眨��@���有些微�r�g影�,我��可以透�^直接修改 GLOBAL wsrep_cluster_address 的值

$ mysql -e "SHOW VARIABLES LIKE 'wsrep_cluster_address';"           ----------------------- ------------------------------------------------- | Variable_name         | Value         |           ----------------------- ------------------------------------------------- | wsrep_cluster_address | gcomm://xxx.xxx.xxx.xx:4567,xxx.xxx.xx.xxx:4567 |           ----------------------- -------------------------------------------------

mysql> SET GLOBAL wsrep_cluster_address='gcomm://192.168.1.100:4567';
Query OK, 0 ROWS affected (3.51 sec)

mysql> SHOW VARIABLES LIKE 'wsrep_cluster_address';
----------------------- ---------------------------
| Variable_name         | VALUE                     |
----------------------- ---------------------------
| wsrep_cluster_address | gcomm://192.168.1.100:4567|
----------------------- ---------------------------

最後需要注意的地方是,由於我��每一台�C器都互相�O定,如果要�P�]全部 Node �C器,��毡�⒌谝惶ㄖ匦略O定 gcomm:// �榭罩担��後�m重新��拥�C器可以先�B上此�C器�M行同步,

�@次把 MySQL Replication 安�b�O定完成,也就是完成架���D的最下面部份

galera cluster for MySQL multi-master replication galera cluster for MySQL multi-master replication

之後��介�B如何透�^ HAproxy �硖�理 MySQL Load Balancer。

mysqldump常用参数和选项

使用mysqldump 导出整个数据库mysqldump -uname -ppasswd --default-character-set=uft8database>database.sql使用mysqldump导出这个数据库的表结构mysqldump -una

MySQL数据库

作者:管理员




现在致电4006-2991-90 OR 查看更多联系方式 →

Go To Top 回顶部