1. 程式人生 > >nginx+mysql雙主搭建

nginx+mysql雙主搭建

說明:mysql雙主架構經過測試在生產環境中使用沒有問題,但是還是建議使用讀寫分離,

 

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解除安裝


[[email protected] mytmp]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[

[email protected] mytmp]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

2 . 解壓安裝mysql

這是為了演示,生產環境參照python一鍵安裝lnmp篇。二進位制安裝mysql。生產環境不要用rpm包安裝,會出問題,測試過

mysql官方網站:http://www.mysql.com

下載 tar -zxf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar

[[email protected] mytmp]# tar -zxf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
[

[email protected] 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 命令可以檢視初始密碼

[[email protected] 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 '[email protected]&*ba3S&e';

mysql> flush privileges;

 


6 . 設定mysql開機啟動

 

開機啟動
systemctl enable mysqld.service

 

 


mysql雙主模式配置:


要實現互為主從,就必須 mster1-->master2設定主從同步 同時 master2--->master1 也設定主從同步

四、Mysql主主同步環境部署

---------mysql1伺服器操作記錄----------

在my.cnf檔案的[mysqld]配置區域新增下面內容:
[[email protected] ~]# 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

[[email protected] ~]# /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 '[email protected]&*ba3S&e';

mysql> alter user 'root'@'localhost' identified by '[email protected]&*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 '[email protected]&*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]配置區域新增下面內容:

[[email protected] ~]# 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

[[email protected] ~]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
mysql> grant replication slave on *.* to [email protected]'10.0.0.%' identified by '[email protected]';
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='[email protected]&*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='[email protected]&*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> 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,"kaifa"),(4,"kaifa");
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 '[email protected]&*ba3S&e';
FLUSH PRIVILEGES;


2.

[[email protected] ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
[[email protected] ~]# tar xf nginx-1.9.9.tar.gz
[[email protected] ~]# cd nginx-1.9.9
[[email protected] nginx-1.9.9]# ./configure --prefix=/usr/local/nginx --with-stream ----with-http_stub_status_module --with-http_ssl_module

[[email protected] nginx-1.9.9]# make
[[email protected] nginx-1.9.9]# make install


--with-stream ---支援TCP的意思

 


[[email protected] conf]# vi /usr/local/nginx/conf/nginx.conf

加入類似於http一樣的模組:


stream {

    upstream mysql {

    hash $remote_addr consistent;

    server 10.1.1.142:3306 weight=5;
    server 10.1.1.106: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 重啟

 

 

測試:

方法一:

在114.115.174.215 這臺上遠端連線nginx伺服器,前提是這臺上也裝有msyql

mysql -uroot [email protected]&*ba3S&e -h 114.115.174.210 -P 7306

方法二:

用navicat連線nginx測試