MySQL做爲世界上使用最爲普遍的數據庫之一,免費是其緣由之一。但不可忽略的是它自己的功能的確很強大。隨着技術的發展,在實際的生產環境中,由單臺MySQL數據庫服務器不能知足實際的需求。此時數據庫集羣就很好的解決了這個問題了。採用MySQL分佈式集羣,可以搭建一個高併發、負載均衡的集羣服務器(這篇博客暫時不涉及)。在此以前咱們必需要保證每臺MySQL服務器裏的數據同步。數據同步咱們能夠經過MySQL內部配置就能夠輕鬆完成,主要有主從複製和主主複製。mysql
主:192.168.182.155 centos7.2 sql
從:192.168.182.156 centos7.2 數據庫
修改主從服務器的配置文件/etc/my.cnf,在mysqld中添加log-bin=mysql-bin開啓二進制文件centos
主服務配置:服務器
[root@localhost ~]# cat /etc/my.cnf # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # This group is read by the server # [mysqld] # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_ignore_db=mysql併發
# # include all files from the config directory # !includedir /etc/my.cnf.d
從服務器配置:負載均衡
[root@localhost ~]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
log-bin=mysql-bin
server-id=2
# Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
在192.168.182.155中建立一個192.168.182.156主機中能夠登陸的MySQL用戶socket
登錄主數據庫分佈式
mysql -uroot -pwc20080512;wordpress
建立鏈接帳號和密碼
MariaDB [(none)]> CREATE USER 'mysql12'@ '192.168.182.156' IDENTIFIED BY 'mysql12';
受權
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'mysql12'@192.168.182.156 IDENTIFIED BY 'mysql12'; Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
執行如下命令鎖定數據庫以防止寫入數據
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.07 sec)
退出mysql命令行,導出數據庫
mysqldump -uroot -pwc20080512 -B heruiguo wanghaixue > /opt/mysql.sql;
scp 數據文件到從服務器
scp /opt/mysql.sql 192.168.182.156:/opt
登錄從服務器導入數據
[root@localhost ~]# mysql -uroot -pwc20080512</opt/mysql.sql
編輯配置文件my.cnf,在[mysqld]下面加入:
server-id=2
重啓數據庫
systemctl restart mariadb.service
查看192.168.182.155 MySQL服務器二進制文件名與位置
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 | 327 | wordpress | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
解鎖數據庫
MariaDB [(none)]> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)
在從服務器執行
[root@localhost ~]# mysql -uroot -pwc20080512
CHANGE MASTER TO
MASTER_HOST='192.168.182.155',
MASTER_USER='mysql12',
MASTER_PASSWORD='mysql12',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000010',
MASTER_LOG_POS=327;
啓動slave進程。
MariaDB [(none)]> START SLAVE; Query OK, 0 rows affected, 1 warning (0.00 sec)
查看主從複製是否配置成功
MariaDB [(none)]> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.182.155 Master_User: mysql12 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000010 Read_Master_Log_Pos: 723 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 929 Relay_Master_Log_File: mysql-bin.000010 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: 723 Relay_Log_Space: 1225 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 1 row in set (0.00 sec)
主從配置完成
測試
在主服務器上建立數據庫
MariaDB [heruiguo]> create database aaa; Query OK, 1 row affected (0.01 sec) MariaDB [heruiguo]> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000012 | 624 | | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
在從庫上驗證
MariaDB [heruiguo]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aaa | | heruiguo | | mysql | | performance_schema | | wanghaixue | +--------------------+ 6 rows in set (0.00 sec)