開篇說明
Galera Cluster 其實可以在每個結點進行讀寫操作,沒有什么讀寫分離的概念。本文重點是講 MariaDB 如何組建 Galera Cluster 環境,以及如何在MyCat中進行MariaDB集群多機聯動的配置。
當然不用MyCAT作為數據庫集群的前置也OK,還可用HA LVS等解決方案。
環境說明
mac os 是宿主機,使用 vmare fusion 開三台 centos 7
mariadb 10.1.10
下載最新版本的 mariadb 去 http://mariadb.org/ 下載sourcecode包
0 其他准備
由於centos7 開始已經使用 firewall 來替換 iptables ,可以用以下命令進行相關操作
# systemctl start firewalld # 啟動,
# systemctl enable firewalld # 開機啟動
# systemctl stop firewalld # 關閉
# systemctl disable firewalld # 取消開機啟動]
firewall-cmd --zone=public --add-port=8080/tcp
更多firewall操作參考 http://www.lmyw.net.cn/?p=596
設定selinux等級,注意如果搞掛了linux啟動不了
vi /etc/selinux/config
SELINUX=disabled
1 配置Linux hosts文件
vi /etc/hosts
在文件中加入:
192.168.31.187 db01 db01.kaye0110.com
192.168.31.188 db02 db02.kaye0110.com
192.168.31.189 db03 db03.kaye0110.com
2 檢查是否已安裝了mariadb 並進行刪除
rpm -qa|grep mariadb
rpm -qa|grep mysql
yum -y remove xxxxx
3 開始安裝mariadb mysql
3.0 安裝相關需要用到插件
yum -y install cmake gcc gcc-c++ autoconf automake zlib* libxml* ncurses ncurses-devel libtool libtool-ltdl-devel* make bison bison-devel openssl-devel libevent-devel libaio libaio-devel pam-devel boost boost-devel valgrind-devel libnl-devel popt-devel popt-static bzr
yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL
yum -y install libev
yum install perl-Digest-MD5
3.1 習慣把應用程度裝到/usr/local/mariadb目錄下
tar xvf mariadb-10.1.10.tar.gz
cd mariadb-10.1.10
3.2 編譯安裝
/*
cmake . -LH –查看cmake支持的mysql相關參數
shell> cmake . -L # overview
shell> cmake . -LH # overview with help text
shell> cmake . -LAH # all params with help text
shell> ccmake . # interactive display
重新編譯時,需要清除舊的對象文件和緩存信息
# make clean
# rm -f CMakeCache.txt
**/
由於當前linux環境下已安裝了一個普通版本的mysql 占用3307 端口
本次安裝時將目錄定為 /usr/local/mariadb/mariadb-10.1.10-13306
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb/mariadb-10.1.10-13306 -DMYSQL_UNIX_ADDR=/usr/local/mariadb/mariadb-10.1.10-13306/socket/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_SPHINX_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_DATADIR=/usr/local/mariadb/mariadb-10.1.10-13306/data -DMYSQL_TCP_PORT=13306 -DENABLED_LOCAL_INFILE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_WSREP=1 -DWITH_INNODB_DISALLOW_WRITES=1
慢慢等一會,時間夠去看一集美劇了。
make -j4
make install
3.3 將目錄權限分配給mysql用戶
chown -R mysql:mysql mariadb-10.1.10-13306
3.4 初始化mariadb mysql
進入mariadb 安裝目錄 ,將通用配置好的mysql config文件復制出來,參數根據不同的環境進行調整 。
/usr/local/mariadb/mariadb-10.1.10-13306> cp ./support-files/my-small.cnf ./my.cnf
執行 scripts/mysql_install_db 腳本初始化。
./mysql_install_db --defaults-file=/usr/local/mariadb/mariadb-10.1.10-13306/my.cnf --basedir=/usr/local/mariadb/mariadb-10.1.10-13306 --datadir=/usr/local/mariadb/mariadb-10.1.10-13306/data --user=mysql
WARNING: The host 'miwifi-r1cm-srv' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MariaDB version. The MariaDB daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MariaDB privileges !
Installing MariaDB/MySQL system tables in '/usr/local/mariadb/mariadb-10.1.10-13306/data' ...
2016-01-24 11:30:31 140448078133312 [Note] /usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld (mysqld 10.1.10-MariaDB) starting as process 29848 ...
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Memory barrier is not used
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Compressed tables use zlib 1.2.7
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Using Linux native AIO
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Using SSE crc32 instructions
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Completed initialization of buffer pool
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Database physically writes the file full: wait...
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-01-24 11:30:32 140448078133312 [Warning] InnoDB: New log files created, LSN=45883
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Doublewrite buffer created
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-24 11:30:32 140448078133312 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Foreign key constraint system tables created
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Tablespace and datafile system tables created.
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Waiting for purge to start
2016-01-24 11:30:32 140448078133312 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.26-76.0 started; log sequence number 0
2016-01-24 11:30:33 140447628183296 [Note] InnoDB: Dumping buffer pool(s) not yet started
OK
Filling help tables...
2016-01-24 11:30:35 139723365349440 [Note] /usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld (mysqld 10.1.10-MariaDB) starting as process 29888 ...
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Memory barrier is not used
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Compressed tables use zlib 1.2.7
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Using Linux native AIO
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Using SSE crc32 instructions
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Completed initialization of buffer pool
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Waiting for purge to start
2016-01-24 11:30:35 139723365349440 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.26-76.0 started; log sequence number 1616799
2016-01-24 11:30:35 139722915084032 [Note] InnoDB: Dumping buffer pool(s) not yet started
OK
Creating OpenGIS required SP-s...
2016-01-24 11:30:37 140529693435968 [Note] /usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld (mysqld 10.1.10-MariaDB) starting as process 29918 ...
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Memory barrier is not used
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Compressed tables use zlib 1.2.7
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Using Linux native AIO
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Using SSE crc32 instructions
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Completed initialization of buffer pool
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Waiting for purge to start
2016-01-24 11:30:38 140529693435968 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.26-76.0 started; log sequence number 1616809
2016-01-24 11:30:38 140529174046464 [Note] InnoDB: Dumping buffer pool(s) not yet started
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqladmin' -u root password 'new-password'
'/usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqladmin' -u root -h miwifi-r1cm-srv password 'new-password'
Alternatively you can run:
'/usr/local/mariadb/mariadb-10.1.10-13306/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr/local/mariadb/mariadb-10.1.10-13306' ; /usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld_safe --datadir='/usr/local/mariadb/mariadb-10.1.10-13306/data'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/mariadb/mariadb-10.1.10-13306/mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Support MariaDB development by buying support/new features from MariaDB
Corporation Ab. You can contact us about this at sales@mariadb.com.
Alternatively consider joining our community based development effort:
http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
3.5 啟動mariadb
cd /usr/local/mariadb/mariadb-10.1.10-13306
/usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld_safe --datadir='/usr/local/mariadb/mariadb-10.1.10-13306/data' &
3.6 登陸mysql client 修改root用戶密碼 , 建立遠程登陸用戶
./mysql -u root -p -P 13306
MariaDB [(none)]> SET PASSWORD FOR root@localhost = PASSWORD('root123');
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> CREATE USER 'root'@'%' IDENTIFIED BY 'root123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> select host,user,password from mysql.user;
+-----------------+------+-------------------------------------------+
| host | user | password |
+-----------------+------+-------------------------------------------+
| localhost | root | *FAAFFE644E901CFAFAEC7562415E5FAEC243B8B2 |
| miwifi-r1cm-srv | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| miwifi-r1cm-srv | | |
| % | root | *FAAFFE644E901CFAFAEC7562415E5FAEC243B8B2 |
+-----------------+------+-------------------------------------------+
7 rows in set (0.00 sec)
4 安裝galera cluster
wget http://cznic.dl.sourceforge.net/project/scons/scons/2.3.5/scons-2.3.5.tar.gz
tar zxvf scons-2.3.5.tar.gz
cd scons-2.3.5
python2.6 setup.py install
下載rpm 安裝包,centos 7 64x 下載地址:http://releases.galeracluster.com/centos/7/x86_64/galera-3-25.3.14-2.el7.x86_64.rpm
rpm -ivh galera-3-25.3.14-2.el7.x86_64.rpm
cp /usr/lib64/galera-3/libgalera_smm.so /usr/local/mariadb/mariadb-10.1.10-13306/lib/plugin/
4.1 galera cluster 同步方法主要有以下幾種
rsync、xtrabackup、mydupm ,這里就講下網上主要推薦的xtrabackup安裝測略
rpm -ivh percona-xtrabackup-2.3.3-1.el7.x86_64.rpm
警告:percona-xtrabackup-2.3.3-1.el7.x86_64.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID cd2efd2a: NOKEY
准備中... ################################# [100%]
正在升級/安裝...
1:percona-xtrabackup-2.3.3-1.el7 ################################# [100%]
5 配置mariadb galera cluster 環境
5.1 新增數據同步專用用戶
mysql> GRANT USAGE ON *.* to dbsync@'%' IDENTIFIED BY 'dbsync123';
mysql> GRANT ALL PRIVILEGES on *.* to dbsync@'%';
mysql> FLUSH PRIVILEGES;
mysql> quit
5.2 調整mariadb my.cnf
進入mariadb目錄 ,建立 my.cnf.d 目錄, 並復制 wsrep.cnf 進入目錄
在my.cnf中加入
!includedir /usr/local/mariadb/mariadb-10.1.10-13306/my.cnf.d
5.3 配置wsrep.cnf
node 1
[mysqld]
wsrep_on=ON
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
innodb_locks_unsafe_for_binlog=1
innodb_flush_log_at_trx_commit = 2 #可以提高性能,galera保證不丟數據
query_cache_size=0
query_cache_type=0
wsrep_provider=/usr/local/mariadb/mariadb-10.1.10-13306/lib/plugin/libgalera_smm.so #修改
wsrep_cluster_name="galera_cluster" #修改
wsrep_cluster_address="gcomm://192.168.31.187,192.168.31.188,192.168.31.189,192.168.31.190" #修改
wsrep_node_address=192.168.31.187 #修改
wsrep_node_name=db01 #修改
wsrep_slave_threads=4
wsrep_certify_nonPK=1
wsrep_max_ws_rows=131072
wsrep_max_ws_size=1073741824
wsrep_debug=0
wsrep_convert_LOCK_to_trx=0
wsrep_retry_autocommit=1
wsrep_auto_increment_control=1
wsrep_drupal_282555_workaround=0
wsrep_causal_reads=0
wsrep_sst_method=xtrabackup #可以修改#rsync,xtrabackup,mysqldump xtrabackup-v2
wsrep_sst_auth= dbsync: dbsync123 #修改
node 2
[mysqld]
wsrep_on=ON
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
innodb_locks_unsafe_for_binlog=1
innodb_flush_log_at_trx_commit = 2 #可以提高性能,galera保證不丟數據
query_cache_size=0
query_cache_type=0
wsrep_provider=/usr/local/mariadb/mariadb-10.1.10-13306/lib/plugin/libgalera_smm.so #修改
wsrep_cluster_name="galera_cluster" #修改
wsrep_cluster_address="gcomm://192.168.31.187,192.168.31.188,192.168.31.189,192.168.31.190" #修改
wsrep_node_address=192.168.31.187 #修改
wsrep_node_name=db01 #修改
wsrep_slave_threads=4
wsrep_certify_nonPK=1
wsrep_max_ws_rows=131072
wsrep_max_ws_size=1073741824
wsrep_debug=0
wsrep_convert_LOCK_to_trx=0
wsrep_retry_autocommit=1
wsrep_auto_increment_control=1
wsrep_drupal_282555_workaround=0
wsrep_causal_reads=0
wsrep_sst_method=xtrabackup #可以修改#rsync,xtrabackup,mysqldump xtrabackup-v2
wsrep_sst_auth= dbsync: dbsync123 #修改
node 3
[mysqld]
wsrep_on=ON
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
innodb_locks_unsafe_for_binlog=1
innodb_flush_log_at_trx_commit = 2 #可以提高性能,galera保證不丟數據
query_cache_size=0
query_cache_type=0
wsrep_provider=/usr/local/mariadb/mariadb-10.1.10-13306/lib/plugin/libgalera_smm.so #修改
wsrep_cluster_name="galera_cluster" #修改
wsrep_cluster_address="gcomm://192.168.31.187,192.168.31.188,192.168.31.189,192.168.31.190" #修改
wsrep_node_address=192.168.31.187 #修改
wsrep_node_name=db01 #修改
wsrep_slave_threads=4
wsrep_certify_nonPK=1
wsrep_max_ws_rows=131072
wsrep_max_ws_size=1073741824
wsrep_debug=0
wsrep_convert_LOCK_to_trx=0
wsrep_retry_autocommit=1
wsrep_auto_increment_control=1
wsrep_drupal_282555_workaround=0
wsrep_causal_reads=0
wsrep_sst_method=xtrabackup #可以修改#rsync,xtrabackup,mysqldump xtrabackup-v2
wsrep_sst_auth= dbsync: dbsync123 #修改
6 啟動並驗證
6.1 啟動首台服務
/usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld_safe --defaults-file=/usr/local/mariadb/mariadb-10.1.10-13306/my.cnf --datadir=/usr/local/mariadb/mariadb-10.1.10-13306/data --log-error=/usr/local/mariadb/mariadb-10.1.10-13306/data/mariadb-error.log --wsrep-cluster-address="gcomm://" &
6.2 啟動其他服務
/usr/local/mariadb/mariadb-10.1.10-13306/bin/mysqld_safe --defaults-file=/usr/local/mariadb/mariadb-10.1.10-13306/my.cnf --datadir=/usr/local/mariadb/mariadb-10.1.10-13306/data --log-error=/usr/local/mariadb/mariadb-10.1.10-13306/data/mariadb-error.log &
6.3 數據庫驗證
6.3.1 參數驗證
主機查詢聯動參數
mysql>SHOW VARIABLES LIKE 'wsrep_cluster_address';
+-----------------------+----------+
| Variable_name | Value |
+-----------------------+----------+
| wsrep_cluster_address | gcomm:// |
+-----------------------+----------+
1 row in set (0.00 sec)
從機查詢聯動參數
MariaDB [(none)]> SHOW VARIABLES LIKE 'wsrep_cluster_address';
+-----------------------+------------------------------------------------------+
| Variable_name | Value |
+-----------------------+------------------------------------------------------+
| wsrep_cluster_address | gcomm://192.168.31.187,192.168.31.212,192.168.31.150 |
+-----------------------+------------------------------------------------------+
1 row in set (0.00 sec)
可以用下面的命令查詢基他配置
MariaDB [(none)]>SHOW STATUS LIKE 'wsrep%';
6.3.2 數據庫操作驗證
主機新建schema
MariaDB [(none)]> create database sync_test;
Query OK, 1 row affected (0.01 sec)
從機create table,並新增記錄
MariaDB [(none)]> use sync_test
Database changed
MariaDB [sync_test]> CREATE TABLE `t_new_table` ( `id` INT NOT NULL, `context` VARCHAR(45) NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.03 sec)
MariaDB [sync_test]> insert into t_new_table (id,context) values ('1','hello world');
Query OK, 1 row affected (0.01 sec)
MariaDB [sync_test]> select * from t_new_table;
+----+-------------+
| id | context |
+----+-------------+
| 1 | hello world |
+----+-------------+
1 row in set (0.00 sec)
在主機 和 3號機上驗證數據
MariaDB [sync_test]> select * from t_new_table;
+----+-------------+
| id | context |
+----+-------------+
| 1 | hello world |
+----+-------------+
1 row in set (0.00 sec)
OK,到這里為止 galera cluster 總算是搭初步建完成, 后面再補 mycat 集成 、HA 集成方案吧。
其他相關文檔可供參考 :
《Galera 10.0.20 on CentOS 6.6》http://blog.csdn.net/yangzhawen/article/details/46788927
《centos6.6下編譯安裝mariadb-10.0.20》 http://blog.csdn.net/ligaofeng/article/details/47173557
《xtrabackup 安裝、備份、還原及錯誤處理 教程》http://blog.163.com/ji_1006/blog/static/10612341201382355716623/
《MySQL的Galera Cluster配置說明》 http://www.360doc.com/content/13/0817/15/834950_307820923.shtml
【2016年1月31日 補充】
今天驗證了數據庫單結點脫離集群環境再加入后數據里補全的驗證,有幾個坑在這里面補充下:
1)wsrep_sst_method=xtrabackup 建議改為 xtrabackup-v2 或直接改為 rsync 模式。
2)如果發現有問題的話,可以看下 innodb backup log 文件(具體文件在datadir目錄下),里面會有具體的數據備份時發現的問題。
3)注意防火牆 5678 端口、4444端口,最簡單的做法是直接把firewall給關掉~~當然生產環境這樣做不是很好。