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)