三台服务器
10.10.10.50 node1 nginx服务器
10.10.10.51 node2 mysql1
10.10.10.52 node3 mysql2
10.10.10.5 keepalived vrrp ip
关闭selunx
[root@node2 ~]# vim /etc/selinux/config SELINUX=disabled [root@node3 ~]# vim /etc/selinux/config SELINUX=disabled
如果安装mysql 出现 Can't create test file /data/node2.lower-test ,说明selinux 忘记关了
关闭iptables
service iptables stop chkconfig iptables off
一 安装设置mysql
[root@node2 ~]# yum -y install mysql-server [root@node3 ~]# yum -y install mysql-server
在node2 mysql设置
手动指定mysql数据目录
[root@node2 ~]# mkdir /data [root@node2 /]# /usr/bin/mysql_install_db --user=mysql --datadir=/data/ [root@node2 ~]# service mysqld start
设置mysql root密码
[root@node2 ~]# 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: 3 Changed: 3 Warnings: 0 mysql> exit [root@node2 ~]# service mysqld stop
如果直接myssqladmin 设置完密码无法进入mysql,如下操作
#mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
开启另外一个新的终端
mysql -u root mysql update user set password=password('root') where user='root'; flush privileges;
配置mysql
[root@node2 ~]# mv /etc/my.cnf /etc/my.cnf.bak [root@node2 ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf [root@node2 ~]# vim /etc/my.cnf [mysqld] datadir = /data/ server-id = 51 #修改id两个服务器必须不同,一般指定为IP最后一段
重起一次mysql 设置mysqladmin密码
[root@node2 ~]# service mysqld stop [root@node2 ~]# service mysqld start [root@node2 ~]# mysqladmin -u root password 'root'
测试输入密码连接
[root@node2 ~]# mysql -uroot -proot
在node3 mysql设置
手动指定mysql数据目录
[root@node3 ~]# mkdir /data [root@node3 /]# /usr/bin/mysql_install_db --user=mysql --datadir=/data/ [root@node3 ~]# service mysqld start
设置mysql root密码
[root@node3 ~]# 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: 3 Changed: 3 Warnings: 0 mysql> exit [root@node3 ~]# service mysqld stop
配置mysql
[root@node3 ~]# mv /etc/my.cnf /etc/my.cnf.bak [root@node3 ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf [root@node3 ~]# vim /etc/my.cnf [mysqld] datadir = /data/ server-id = 52 #修改id两个服务器必须不同,一般指定为IP最后一段
重起一次mysql 设置mysqladmin密码
[root@node3 ~]# service mysqld stop [root@node3 ~]# service mysqld start [root@node3 ~]# mysqladmin -u root password 'root'
测试输入密码连接
ot@node3 ~]# mysql -uroot -proot
配置mysql主主
创建同步账号 格式如下
grant replication slave on *.* to '帐号' @ '从服务器IP' identified by '密码';
在两个mysql服务器创建同步账号 rep 密码 rep
# 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的 状态
mysql> show master status;
File mysql-bin.000001
Position 391
填入对应的sql参数
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)
启动同步
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的 状态
mysql> show master status;
File mysql-bin.000001
Position 386
填入对应的sql参数
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)
启动同步
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
查看同步状态
mysql> show slave status \G
验证同步设置
在mysql1创建一个数据库wordpress和一个表test 并插入一个字段
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查询
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 一个值
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查询
mysql> select * from test; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec)
同步成功
二 安装keepalived
yum -y install keepalived
授权mysql允许使用三个ip 连接
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 配置文件
[root@node2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] 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 虚拟IP VIP } } 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 } } }
复制配置文件到另一服务器
[root@node2 ~]# scp /etc/keepalived/keepalived.conf 10.10.10.52:/etc/keepalived/keepalived.conf
[root@node3 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] 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 的内容 ,在两个服务器执行
[root@node2 ~]# mkdir /data/script [root@node2 ~]# vim /data/script/keepalived.sh #!/bin/bash service keepalived stop [root@node2 ~]# chmod +x /data/script/keepalived.sh
完成后 在两个服务器启动mysqld 和 keepalived
service mysqld start service keepalived start
查看IP状态 ,VIP 在node2
[root@node2 ~]# 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 [root@node3 ~]# 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 连接
mysql -h10.10.10.5 -uroot -proot mysql
停止mysql1
[root@node2 ~]# service mysqld stop
查看IP状态 VIP 在node3
[root@node2 script]# 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 [root@node3 ~]# 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 开机启动
chkconfig mysqld on chkconfig keepalived on
三 安装wordpress
yum -y install httpd php php-mysql service httpd start wget https://cn.wordpress.org/wordpress-4.3.1-zh_CN.zip unzip wordpress-4.3.1-zh_CN.zip cp -rf wordpress/ /var/www/html/
打开 http://10.10.10.51/wordpress/wp-admin/setup-config.php
设置数据库连接后复制生产的wp-config.php粘贴
vim /var/www/html/wp-config.php
复制配置文件到第二台服务器
scp /var/www/html/wp-config.php 10.10.10.52:/var/www/html/
打开第二台服务器,可以直接查看
http://10.10.10.52/wordpress
四 安装nginx
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
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_name localhost; location / { proxy_pass http://wordpress; root /wordpress; index index.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