本次实验 需要3台centos6.5
192.168.204.139 #【主】mysql
192.168.204.138 #【从】mysql
192.168.294.129 #lamp 网站
请关闭3台机器的防火墙,和SELinux
# 关闭防火墙
/etc/init.d/iptables stop
chkconfig iptables off
chkconfig --list |grep iptables
# 关闭SELinux
sed -i /SELINUX/s/enforcing/disabled/g /etc/selinux/config
==================================================
# 配置mysql【主】服务器192.168.204.139
yum install mysql-server mysql -y
备份mysql配置文件
cp /etc/my.cnf /etc/my.cnf_bak20170915
# 修改/etc/my.cnf如下:
------------------------------------------
[mysqld]
datadir = /data/mydata
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
log-bin=mysql-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log_error=/var/log/mysqld.err
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db=all
------------------------------------------
创建/data/mysql 数据目录,
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
# 重启mysql即可,
/etc/init.d/mysqld restart
******如果重启mysql失败,请查看SELinux是否关闭******
mysql默认没有密码,命令行输入mysql即可进入数据库
# 在【主】服务器上设置权限,执行如下命令:
> grant replication slave on *.* to tongbu@'%' identified by '123123';
> flush privileges;
> show master status;
此处应该有【配图】
# 配置mysql从【从】服务器 192.168.204.138
yum install mysql-server mysql -y
mv /etc/my.cnf /etc/my.cnf.bak
vim /etc/my.cnf
------------------------------------------
[mysqld]
datadir = /data/mydata
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
log-bin=mysql-bin
#
server-id=2
auto_increment_offset=2
#
auto_increment_increment=2
[mysqld_safe]
log_error=/var/log/mysqld.err
pid-file=/var/run/mysqld/mysqld.pid
#
master-host = 192.168.204.139
master-user = tongbu
master-pass = 123123
master-port = 3306
master-connect-retry = 60
#
replicate-do-db=all
------------------------------------------
重启mysql数据库
/etc/init.d/mysqld restart
******如果重启mysql失败,请查看SELinux是否关闭******
然后 【从】服务器上 指定master IP 和同步的pos 点;
> change master to master_host='192.168.204.139', master_user='tongbu', master_password='123123', master_log_file='mysql-bin.000003', master_log_pos=415;
> slave start;
> show slave statusG;
# 如果 Slave_IO_Running: Yes, Slave_SQL_Running: Yes
==================================================
****** vim 常用命令 ******
# 命令行模式
:%d #清空文件内容
:%s/wk/wkui/g #g 全部替换
:%s/My/ my/g #替换
:%s/My/& /g #My 后面加了一个空格
:%s/^/& /g #开头加个空格
:%s/$/& /g #结尾加个空格
==================================================
###另外配置 lamp,192.168.204.129
yum install httpd httpd-devel php php-mysql php-devel mysql-server mysql mysql-devel -y
# 关闭【防火墙】和【SELinux】
chkconfig --del iptables
/etc/init.d/iptables stop
sed -i /SELINUX/s/enforcing/disabled/g /etc/selinux/config
# 默认的网站发布目录/var/www/html
cd /root/
wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip
# 解压discuz 程序包:
unzip Discuz_X3.2_SC_UTF8.zip -d /var/www/html/discuz
vim /etc/httpd/conf.d/discuz.conf # httpd 默认会读取conf.d 目录下的文件
输入如下内容: 如果你是httpd-2.4 版本,还要输入《Directory……》
------------------------------------
<VirtualHost *:80>
DocumentRoot /var/www/discuz/upload/
</VirtualHost>
------------------------------------
重启httpd
/etc/init.d/httpd restart
# 访问ip 开始安装discuz 论坛
啊哈,好多红XXX 文件权限不够啦
chmod -R 777 /var/www/html/discuz/upload/ # 这是图省事,才全部777的,实际生产中切不可这样,否则后果自负
***** 在192.168.204.139【主】mysql 新建数据库 *****
> create database discuz charset=utf8;
> grant all on discuz.* to discuz@'192.168.204.129' identified by '123123';
安装discuz 时指定用192.168.204.139 的数据库discuz,【配图】
安装完毕查看【从】mysql:192.168.204.138,是否有discuz数据库【配图】
# 看看discuz 的配置信息
/var/www/html/upload/config/config_global.php
/var/www/html/upload/config/config_ucenter.php
==================================================
关闭【主】mysql,再次访问网站,网站还是挂了
虽然做了主从了,但是一旦主mysql 挂了,网站照样不可以访问,你就只能哈哈了,
如何才能不受影响呢,那就是keepalived,高可用,yeah
# keepalived + mysql 主从
http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
tar xf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-696.6.3.el6.x86_64/ && make && make install
# make 时候报错了,yum install popt popt-devel -y
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -p /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
# 修改【主】服务器上的 keepalived.conf 配置文件,
vim /etc/keepalived/keepalived.conf
------------------------------------------
!Configuration File For keepalived
global_defs {
notification_email {
1006793841@qq.com
}
notification_email_from 1006793841@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.204.150
}
}
virtual_server 192.168.204.150 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
#
real_server 192.168.204.139 3306 {
#
weight 100
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
------------------------------------------
MySQL【从】服务器配置 keepalived.conf 和master 一样,
但是需要修改三处:
1、把 Realserver IP修改为 real_server 192.168.204.138;
2、优先级从 priority 100 给为 90;
3、级别 state MASTER 改为 BACKUP
在 master、slave 数据库上创建/data/sh/mysql.sh 脚本,内容为:
/etc/init.d/keepalived stop
# 上面的命令会在mysql服务停止后,停止keepalived服务
然后分别重启两台数据库上的keepalived 服务即可。
最后测试停止master MySQL 服务,是否会自动切换到 Backup 上。
mysql 优化还可以进行读写分离、mysql+DRBD、拆分表等等优化。继续研究吧!
==================================================
****** 一些问题 ******
# 你的机器可能缺少 ip_vs 模块
# 挂载ip_vs
modprobe ip_vs
lsmod |grep ip_vs
/etc/init.d/keepalived restart
# 远程复制【主】mysql 的keepalived 配置文件
scp -r keepalived.conf root@192.168.204.138:/etc/keepalived/
启动keepalived,
/etc/init.d/keepalived start
查看机器ip地址,你会发现一个新 IP:192.168.204.150
为【主】mysql数据库,添加新用户bbs
==================================================
***** keepalived + mysql主从 *****
远程192.168.204.129,操作如下:
# 重新部署 网站discuz,输入虚拟ip 192.168.204.150
# 部署成功后,新注册两个用户
登录用户
# 停止【主】mysql,刷新网站看看是否正常???