mysql双主架构
注意:最好不要用innodedb来同步数据库,要用databus来同步数据库,数据量大要用上mycat中间件 Mysql主主同步环境部署: centos 7.4 三台云主机: mysql1 :10.1.1.142 mysql2 :10.1.1.106 nginx: 10.1.1.152 外网地址:114.115.174.210 两台都安装mysql 1 . 安装新版mysql前,需将系统自带的mariadb-lib卸载 [root@slave mytmp]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 [root@slave mytmp]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 2 . 解压安装mysql mysql官方网站:http://www.mysql.com 下载 tar -zxf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar [root@slave mytmp]# tar -zxf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar [root@slave mytmp]# ls mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar mysql-community-libs-5.7.16-1.el7.x86_64.rpm mysql-community-client-5.7.16-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm mysql-community-common-5.7.16-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.16-1.el7.x86_64.rpm mysql-community-devel-5.7.16-1.el7.x86_64.rpm mysql-community-server-5.7.16-1.el7.x86_64.rpm mysql-community-embedded-5.7.16-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.16-1.el7.x86_64.rpm mysql-community-test-5.7.16-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.16-1.el7.x86_64.rpm 使用rpm -ivh命令依次进行安装 rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm 3 . 登录到mysql,更改root用户的密码 通过 cat ~/.mysql_secret 命令可以查看初始密码 [root@slave mytmp]# mysql -uroot -p Enter password: mysql> set password=password('1234'); 4 . 创建用户,及作权限分配 mysql> CREATE USER 'zz'@'%' IDENTIFIED BY '1234'; mysql> GRANT ALL PRIVILEGES ON *.* TO 'zz'@'%'; mysql> FULSH PRIVILEGES; 5 . 远程登陆授权 mysql> grant all privileges on *.* to 'root'@'%' identified by 'j0!uN@Da12tA&*ba3S&e'; mysql> flush privileges; 6 . 设置mysql开机启动 开机启动 systemctl enable mysqld.service mysql双主模式配置: 要实现互为主从,就必须 mster1-->master2设置主从同步 同时 master2--->master1 也设置主从同步 四、Mysql主主同步环境部署 ---------mysql1服务器操作记录---------- 在my.cnf文件的[mysqld]配置区域添加下面内容: [root@master1 ~]# vim /usr/local/mysql/my.cnf server-id = 1 log-bin = mysql-bin sync_binlog = 1 binlog_checksum = none binlog_format = mixed auto-increment-increment = 2 auto-increment-offset = 1 slave-skip-errors = all [root@master1 ~]# /etc/init.d/mysql restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS! 创建一个复制用户 出了小问题,由于之前root用户的密码设置过于简单在创建复制用户时报如下错误 mysql> grant replication slave on *.* to 'root'@'10.1.1.%' identified by 'j0!uN@Da12tA&*ba3S&e'; mysql> alter user 'root'@'localhost' identified by 'j0!uN@Da12tA&*ba3S&e'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to 'root'@'10.1.1.%' identified by 'j0!uN@Da12tA&*ba3S&e'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 锁表,待同步配置完成在解锁 mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) 查看当前的binlog以及数据所在位置 mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000006 | 996 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) -------master2服务器操作记录------- 在my.cnf文件的[mysqld]配置区域添加下面内容: [root@master2 ~]# vim /usr/local/mysql/my.cnf server-id = 2 log-bin = mysql-bin sync_binlog = 1 binlog_checksum = none binlog_format = mixed auto-increment-increment = 2 auto-increment-offset = 2 slave-skip-errors = all [root@master2 ~]# /etc/init.d/mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! mysql> grant replication slave on *.* to repl@'10.0.0.%' identified by '1qaz@WSX'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) ? mysql> flush tables with read lock; ? Query OK, 0 rows affected (0.00 sec) 查看 master情况 mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 150 | | | | +------------------+----------+--------------+------------------+-------------------+ 分别开启同步对方 ---------------master1服务器做同步操作--------------- mysql> unlock tables; //先解锁,将对方数据同步到自己的数据库中 mysql> stop slave; mysql> change master to master_host='10.1.1.142',master_user='root',master_password='j0!uN@Da12tA&*ba3S&e',master_log_file='mysql-bin.000002',master_log_pos=150; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) ? 查看两个线程状态是否为YES? mysql> show slave status \G; Slave_IO_Running: Yes Slave_SQL_Running: Yes -------------master2服务器做同步操作--------------- mysql> unlock tables; //先解锁,将对方数据同步到自己的数据库中 mysql> slave stop; mysql> change master to master_host='10.1.1.106',master_user='root',master_password='j0!uN@Da12tA&*ba3S&e',master_log_file='mysql-bin.000002',master_log_pos=150; Query OK, 0 rows affected, 2 warnings (0.06 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status \G; Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 996 Relay_Log_File: master2-relay-bin.000002 Relay_Log_Pos: 312 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes 以上表明双方已经实现了mysql主主同步。 在master1数据库上写入新数据 mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql> create database test; Query OK, 1 row affected (0.01 sec) mysql> grant all privileges on test.* TO 'root'@'%' identified by 'jenkins@123' with grant option; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> use test; Database changed mysql> create table if not exists kaifa ( id int(10) PRIMARY KEY AUTO_INCREMENT, name varchar(50) NOT NULL); Query OK, 0 rows affected (0.04 sec) mysql> insert into kaifa values(2,'join'); Query OK, 1 row affected (0.00 sec) mysql> insert into kaifa values(1,'bob'); Query OK, 1 row affected (0.00 sec) mysql> select * from kaifa; +----+-----------+ | id | name | +----+-----------+ | 1 | bob | | 2 | join | +----+-----------+ 2 rows in set (0.00 sec) 然后在master2数据库上查看,发现数据已经同步过来了! mysql> select * from test.kaifa; +----+-----------+ | id | name | +----+-----------+ | 1 | bob | | 2 | join | +----+-----------+ 2 rows in set (0.00 sec) 2)在master2数据库上写入新数据 mysql> create database ceshi; Query OK, 1 row affected (0.00 sec) mysql> insert into test.kaifa values(3,'nginx'),(4,'hadoop'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 然后在master1数据库上查看,发现数据也已经同步过来了! mysql> show databases;+--------------------+ | Database | +--------------------+ | information_schema | | ceshi | | test | | mysql | | performance_schema | | test | +--------------------+6 rows in set (0.00 sec) mysql> select * from test.kaifa; +----+-----------+ | id | name | +----+-----------+ | 1 | bob | | 2 | join | | 3 | kaifa | | 4 | kaifa | +----+-----------+ 4 rows in set (0.00 sec) 至此,Mysql主主同步环境已经实现。 nginx搭建; 1.在两台数据库添加权限 GRANT ALL ON *.* TO 'root'@'10.1.1.152.%' IDENTIFIED BY 'j0!uN@Da12tA&*ba3S&e'; FLUSH PRIVILEGES; 2. [root@ecs-01 ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz [root@ecs-01 ~]# tar xf nginx-1.9.9.tar.gz [root@ecs-01 ~]# cd nginx-1.9.9 [root@ecs-01 nginx-1.9.9]# ./configure --prefix=/usr/local/nginx --with-stream ----with-http_stub_status_module --with-http_ssl_module [root@ecs-01 nginx-1.9.9]# make [root@ecs-01 nginx-1.9.9]# make install --with-stream ---支持TCP的意思 [root@ecs-01 conf]# vi /usr/local/nginx/conf/nginx.conf 加入类似于http一样的模块: stream { upstream mysql { #hash $remote_addr consistent; server 10.1.1.124:3306 weight=5; server 10.1.1.204:3306 weight=5; } server { listen 3306; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass mysql; } } /usr/local/nginx/sbin/nginx 启动 /usr/local/nginx/sbin/nginx -s reload 重启 测试: 在192.168.43.75 这台上远程连接nginx服务器 mysql -uroot -pj0!uN@Da12tA&*ba3S&e -h 192.168.88 -P 7306