23112312 发表于 2015-11-16 10:05:41

lnmp nginx mysql双主+keepalived

三台服务器
10.10.10.50node1    nginx服务器
10.10.10.51node2    mysql1
10.10.10.52node3    mysql2
10.10.10.5    keepalived vrrp ip

关闭selunx

1
2
3
4
# vim /etc/selinux/config
SELINUX=disabled
# vim /etc/selinux/config
SELINUX=disabled




如果安装mysql 出现 Can't create test file /data/node2.lower-test ,说明selinux 忘记关了
关闭iptables


1
2
service iptables stop
chkconfig iptables off




一 安装设置mysql

1
2
# yum -y install mysql-server
# yum -y install mysql-server




在node2 mysql设置
手动指定mysql数据目录

1
2
3
# mkdir /data
# /usr/bin/mysql_install_db --user=mysql --datadir=/data/
# service mysqld start




设置mysql root密码

1
2
3
4
5
6
7
8
# mysql
mysql> use mysql
Database changed
mysql> update user set password=password('root') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3Changed: 3Warnings: 0
mysql> exit
# service mysqld stop




如果直接myssqladmin 设置完密码无法进入mysql

1
#mysqld_safe --user=mysql --skip-grant-tables --skip-networking &




开启另外一个新的终端

1
2
3
mysql -u root mysql
update user set password=password('root') where user='root';
flush privileges;




配置mysql

1
2
3
4
5
6
# mv /etc/my.cnf /etc/my.cnf.bak
# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
# vim /etc/my.cnf

datadir         = /data/
server-id       = 51             #修改id两个服务器必须不同,一般指定为IP最后一段




重起一次mysql 设置mysqladmin密码

1
2
3
# service mysqld stop
# service mysqld start
# mysqladmin -u root password 'root'




测试输入密码连接

1
# mysql -uroot -proot




在node3 mysql设置
手动指定mysql数据目录


1
2
3
# mkdir /data
# /usr/bin/mysql_install_db --user=mysql --datadir=/data/
# service mysqld start




设置mysql root密码

1
2
3
4
5
6
7
8
# mysql
mysql> use mysql
Database changed
mysql> update user set password=password('root') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3Changed: 3Warnings: 0
mysql> exit
# service mysqld stop




配置mysql

1
2
3
4
5
6
# mv /etc/my.cnf /etc/my.cnf.bak
# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
# vim /etc/my.cnf

datadir         = /data/
server-id       = 52             #修改id两个服务器必须不同,一般指定为IP最后一段




重起一次mysql 设置mysqladmin密码

1
2
3
# service mysqld stop
# service mysqld start
# mysqladmin -u root password 'root'




测试输入密码连接

1
ot@node3 ~]# mysql -uroot -proot




配置mysql主主
创建同步账号 格式如下

1
2
3
grant replication slave
on *.*
to '帐号' @ '从服务器IP' identified by '密码';




在两个mysql服务器创建同步账号 rep 密码 rep

1
2
3
4
5
6
7
# mysql -uroot -proot
mysql> use mysql
Database changed
mysql>grant replication slave
on *.*
to 'rep'@'%' identified by 'rep';
mysql> select * from user where user = 'rep' \G;




在mysql2服务器执行同步
先查询mysql1的 状态

1
mysql> show master status;





File             mysql-bin.000001
Position   391   
填入对应的sql参数

1
2
mysql> change master to master_host='10.10.10.51',master_user='rep',master_password='rep',master_log_file='mysql-bin.000001',master_log_pos=391;
Query OK, 0 rows affected (0.03 sec)




启动同步

1
2
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)




查看同步状态
mysql> show slave status \G

Slave_IO_Running: Yes
Slave_SQL_Running: Yes同步成功

在mysql1服务器执行同步
先查询mysql2的 状态

1
mysql> show master status;





File             mysql-bin.000001
Position   386
填入对应的sql参数

1
2
mysql> change master to master_host='10.10.10.52',master_user='rep',master_password='rep',master_log_file='mysql-bin.000001',master_log_pos=386;
Query OK, 0 rows affected (0.03 sec)




启动同步

1
2
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)




查看同步状态

1
mysql> show slave status \G






验证同步设置
在mysql1创建一个数据库wordpress和一个表test 并插入一个字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> use wordpress
Database changed
mysql> create table test(id int(3));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values (1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)




在mysql2查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| mysql            |
| test               |
| wordpress          |
+--------------------+
4 rows in set (0.00 sec)
mysql> use wordpress;
Database changed
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)




在mysql2 插入wordpress test 一个值

1
2
3
4
5
6
7
8
9
10
11
mysql> insert into test values (2);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)




在mysql1查询

1
2
3
4
5
6
7
8
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)




同步成功

二   安装keepalived

1
yum -y install keepalived




授权mysql允许使用三个ip 连接

1
2
3
4
mysql -uroot -proot
mysql> grant all on *.* to root@'10.10.10.5' identified by 'root';
mysql>grant all on *.* to root@'10.10.10.51' identified by 'root';
mysql>grant all on *.* to root@'10.10.10.52' identified by 'root';




编缉keepalived 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER            #主服务器
    interface eth0
    virtual_router_id 51
    priority 100            #优先级
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      10.10.10.5            #VRRP 虚拟IPVIP
    }
}
virtual_server 10.10.10.5 3306 {   #连接的端口
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 10.10.10.51 3306 {   #本机服务的IP端口
      weight 1
      notify_down /data/script/keepalived.sh   #检测失败执行的脚本,停止keepalive
      TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_prot 3306
      }
    }
}




复制配置文件到另一服务器

1
# scp /etc/keepalived/keepalived.conf 10.10.10.52:/etc/keepalived/keepalived.conf





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP            #修改为backup服务器
    interface eth0
    virtual_router_id 51
    priority 90               #优先级改小
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      10.10.10.5
    }
}
virtual_server 10.10.10.5 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 10.10.10.52 3306 {   #本机服务的IP端口
      weight 1
      notify_down /data/script/keepalived.sh   #检测失败执行的脚本,停止keepalive
      TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_prot 3306
      }
    }
}




/data/script/keepalived.sh 的内容 ,在两个服务器执行

1
2
3
4
5
# mkdir /data/script
# vim /data/script/keepalived.sh
#!/bin/bash
service keepalived stop
# chmod +x/data/script/keepalived.sh




完成后 在两个服务器启动mysqld 和 keepalived

1
2
service mysqld start
service keepalived start




查看IP状态 ,VIP 在node2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ip addr list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:05:71:e9 brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.51/24 brd 10.10.10.255 scope global eth0
    inet 10.10.10.5/32 scope global eth0
    inet6 fe80::20c:29ff:fe05:71e9/64 scope link
       valid_lft forever preferred_lft forever
# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:80:b9:0a brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.52/24 brd 10.10.10.255 scope global eth0
    inet6 fe80::20c:29ff:fe80:b90a/64 scope link
       valid_lft forever preferred_lft forever




测试mysql 连接

1
mysql -h10.10.10.5 -uroot -proot mysql




停止mysql1

1
# service mysqld stop




查看IP状态VIP 在node3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ip addr show   
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:05:71:e9 brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.51/24 brd 10.10.10.255 scope global eth0
    inet6 fe80::20c:29ff:fe05:71e9/64 scope link
       valid_lft forever preferred_lft forever
# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:80:b9:0a brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.52/24 brd 10.10.10.255 scope global eth0
    inet 10.10.10.5/32 scope global eth0
    inet6 fe80::20c:29ff:fe80:b90a/64 scope link
       valid_lft forever preferred_lft forever




设置mysqld 和 keepalived 开机启动

1
2
chkconfig mysqld on
chkconfig keepalived on





三 安装wordpress

1
2
3
4
5
yum -y install httpd php php-mysql
service httpd start
wgethttps://cn.wordpress.org/wordpress-4.3.1-zh_CN.zip
unzip wordpress-4.3.1-zh_CN.zip
cp -rfwordpress/ /var/www/html/




打开 http://10.10.10.51/wordpress/wp-admin/setup-config.php
设置数据库连接后复制生产的wp-config.php粘贴
vim /var/www/html/wp-config.php
复制配置文件到第二台服务器

1
scp /var/www/html/wp-config.php 10.10.10.52:/var/www/html/




打开第二台服务器,可以直接查看
http://10.10.10.52/wordpress

四 安装nginx

1
2
3
4
5
wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

yum install nginx




配置nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vim /etc/nginx/conf.d/default.conf
upstream wordpress {
    server 10.10.10.51:80;
    server 10.10.10.52:80;
}
server {
    listen       80;
    server_namelocalhost;
    location / {
      proxy_pass http://wordpress;
      root   /wordpress;
      indexindex.html index.htm;
    }
    error_page   500 502 503 504/50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
}




打开 http://10.10.10.50/wordpress/
关闭node2 的 mysql 自动切换到 node3
关闭node3的mysql, 连接数据库出错


mysql 在node2停止后keepalived在node2 执行脚本停止自已 ,服务自动切换到node3
node3 停止后 ,node2的 keepalived 和mysqld要手动启动
没有开启vrrp抢占 ,node2 的keepalived恢复后 ,VRRP不会自动切换到node2,数据库连接依然在node3



页: [1]
查看完整版本: lnmp nginx mysql双主+keepalived