上篇详解了CentOS安装LNMP+Mongodb生产环境,现在详解它们的部署,多数已经在安装时执行过了,此处主要做解释以及常用维护:
============================================= mysql
#用户组
groupadd mysql
#用户
useradd -g mysql mysql -s /bin/false
#MySQL数据库存放目录
/home/mysql/data
#MySQL运行目录
/usr/local/mysql
#配置文件
/etc/my.cnf
#服务脚本
/etc/rc.d/init.d/mysqld
#通信文件
/var/lib/mysql/mysql.sock
#改默认密码sql
alter user 'root'@'localhost' identified by 'sa';
#允许root远程登录,改密码为'sa'的sql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'sa' WITH GRANT OPTION;
flush privileges;
#禁止root远程登录的sql
delete from mysql.user where host<>'localhost' and user='root';
flush privileges;
#读写账号
#master 写
>mysql grant select,insert,update,delete,create,alter,index on mydb.* to 'web'@'%' identified by '123456';
>mysql flush privileges;
#slave 读
>mysql grant select on mydb.* to 'web'@'%' identified by '123456';
>mysql flush privileges;
============================================= nginx
#用户组
groupadd www
#用户
useradd -g www www -s /bin/false
#运行目录
/usr/local/nginx
#服务脚本
/etc/rc.d/init.d/nginx
#配置文件
/usr/local/nginx/conf/nginx.conf
#网站根目录
/usr/local/nginx/html/
============================================= php
#运行目录
/usr/local/php
#ini文件,带软链接
ln -s /usr/local/php/etc/php.ini /etc/php.ini
#配置文件,带软链接
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
#服务脚本
/etc/rc.d/init.d/php-fpm
#安装php扩展,比如“xxx”;所有此般编译后的so都应在/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
cd xxx
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
vi /usr/local/php/etc/php.ini #加入 extension="xxx.so"
#memcache服务运行目录
/usr/local/memcached
#memcache服务脚本
/etc/init.d/memcached
============================================= MongoDB
#用户组
groupadd mongodb
#用户
useradd -g mongodb mongodb -s /bin/false
#运行目录
/usr/local/mongodb
#数据目录
mkdir -p /home/mongodb/db
#日志目录
mkdir -p /home/mongodb/log
#配置文件
/usr/local/mongodb/mongodb.conf
#pid路径
/usr/local/mongodb/mongo.pid
#服务脚本
/etc/rc.d/init.d/mongod
#【!!坑!!】重启mongodb进程后,必须重启php-fpm、httpd、java等所有已连接客户端的进程,否则返回“Remote server has closed the connection”
#启动MongoDB
mongo #进入MongoDB控制台
show dbs #查看默认数据库
use admin #切换到admin数据库
exit #退出MongoDB控制台
#创建索引
>mongo db.table01.ensureIndex({"myid":1});
============================================== sphinx
#运行目录
/usr/local/sphinx
#配置文件
/usr/local/sphinx/etc/sphinx.conf
#索引文件存放目录
/home/sphinx
#服务脚本
/etc/rc.d/init.d/sphinx
#启动进程
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf
#重建索引
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all --rotate
#停止索引和进程
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf --stop
#如果sphinx文件太大,删除对应存放目录文件再重建增量索引文件夹
cd /home/sphinx/
rm * -fr
mkdir indexdelta
# 如果sphinx服务无法启动或者启动有错误,可尝试重命名 mv /usr/local/sphinx/var/data/binlog.meta文件,再重启则ok
============================================== jre
yum install java-1.8.0-openjdk-devel
vi /etc/profile
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_71
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
:wq!
source /etc/profile
============================================== mysql 同步
# 读写账号
master 写
grant select,insert,update,delete,create,alter,index on sqtdb.* to 'web'@'%' identified by '123456';
flush privileges;
slave 读
grant select on sqtdb.* to 'web'@'%' identified by '123456';
flush privileges;
********* master vi /etc/my.cnf
[mysqld]
server-id=100
binlog-format=mixed
log-bin=mysql-bin
max_binlog_size=1000M
binlog-do-db=mydb
#binlog-ignore-db=mysql
#binlog-ignore-db=information_schema
#binlog-ignore-db=performance_schema
#binlog-ignore-db=sys
#root登录master建立同步账号
>mysql grant replication slave on *.* to 'replc'@'%' identified by '123456';
********* slave vi /etc/my.cnf
[mysqld]
server-id=200
binlog-format=mixed
log-bin=mysql-bin
max_binlog_size=1000M
binlog-do-db=mydb
#binlog-ignore-db=mysql
#binlog-ignore-db=information_schema
#binlog-ignore-db=performance_schema
#binlog-ignore-db=sys
#root 登录slave
>mysql change master to master_host='192.168.1.100',master_user='replc',master_password='123456',master_connect_retry=100;
>mysql show slave status\G; #查看同步是否主要是查看Slave_IO_Running与Slave_SQL_Running选项。如果正常同步,这两选必须同时为“YES”。
#如果启动slave时报错 ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
>mysql reset slave; # 先reset(慎用!先记下slave当前日志文件名和位置,如果reset后日志名和位置不对,则直接用stop再change master)
>mysql start slave; # 之后可以使用start 启动了
#如果要重新同步,先在master上执行>mysql flush logs; 然后 >mysql show master status; 得到File、Position,再到slave执行以下语句:
>mysql stop slave;
>mysql change master to master_host='192.168.1.100',master_user='replc',master_password='123456',master_log_file="localhost-bin.000004",master_log_pos=659,master_connect_retry=100;
>mysql start slave;
# 完全重新同步办法
----slave
>mysql stop slave;
----master
>mysql flush tables with read lock; #先锁表 !!!
>mysql show master status\G;
mysql-bin.000021 | 47529419
# 使用大批量的导出方式,导入也随之加速,如果数据大就带参数gzip
/usr/local/mysql/bin/mysqldump -uroot -p --default-character-set=utf8 -e --max_allowed_packet=41943040 --net_buffer_length=41043040 mydb|gzip > /home/mysql/mydb20160606.sql.gz
----slave
scp root@192.168.1.100:/home/mysql/mydb20160606.sql.gz /home/mysql/mydb20160606.sql.gz
gzip -d -c /home/mysql/mydb20160606.sql.gz > /home/mysql/mydb20160606.sql
>mysql drop database mydb;
>mysql create database `mydb` character set utf8 collate utf8_general_ci;
mysql -uroot -p mydb</home/mysql/mydb20160606.sql
----master
>mysql unlock tables; #slave导入完成再unlock !!!
----slave
>mysql change master to master_host='192.168.1.100',master_user='replc',master_password='123456',master_log_file="mysql-bin.000021",master_log_pos=47529419,master_connect_retry=100;
>mysql start slave; #如果有“Slave failed to initialize relay log info structure from the repository”错误先 reset slave再 change master ……
>mysql show slave status\G;
============================================== mongodb 主从同步(推荐转用副本集模式)
# master 修改启动脚本
vi /etc/init.d/mongod
/usr/local/mongodb/bin/mongod --maxConns 20000 --config /usr/local/mongodb/mongodb.conf --master
# slave 修改启动脚本
vi /etc/init.d/mongod
/usr/local/mongodb/bin/mongod --maxConns 20000 --config /usr/local/mongodb/mongodb.conf --slave --source 192.168.1.100:27017
# 【注意】从节点slave是只读,不能提供写操作的;如果写操作要切换到slave,slave必须先停止mongo进程,再修改mongo启动脚本为master模式,最后重新启动mongo进程;
# 增加slave只可在业务影响小的时候操作,否则slave首次同步时的锁表会影响master的读取;
# 【切记】重启mongodb进程后,必须重启php-fpm、httpd、java等所有已连接客户端的进程,否则返回“Remote server has closed the connection”
============================================= apache2+ftp server(选装)
#用户和组
User apache
Group apache
#运行
/usr/sbin/httpd
#配置
/etc/httpd/conf/httpd.conf
#网站根
/var/www/html
#systemctl服务
/usr/lib/systemd/system/httpd.service
systemctl enable httpd.service
#安装vsftpd
yum -y install vsftpd
vi /etc/vsftpd/vsftpd.conf
local_root = /var/www/html
useradd myftp -s /sbin/nologin -d /var/www/html -g ftp
passwd myftp #设ftp密码
chown -R myftp /var/www/html
chmod -R 777 /var/www/html
systemctl enable vsftpd.service
vi /etc/sysconfig/iptables # 80端口看实际情况设置
-A INPUT -m state --state NEW -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
vi /etc/sysconfig/iptables-config # 增加2项
IPTABLES_MODULES="ip_conntrack_ftp"
IPTABLES_MODULES="ip_nat_ftp"
systemctl restart iptables.service
============================================= 修改服务器ip、mysql账户,则修改对应文件和sql
#如果mongodb绑定了ip,则更改
vi /usr/local/mongodb/mongodb.conf
#如果master的同步账号也更换了,也要随之更改以下sql语句中的同步账号:
先在master上执行>mysql flush logs; 然后 >mysql show master status; 得到File、Position,再到slave上执行以下语句:
>mysql stop slave;
>mysql change master to master_host='新的ip',master_user='replc',master_password='123456',master_log_file="localhost-bin.000004",master_log_pos=659,master_connect_retry=100;
>mysql start slave;
>mysql show slave status\G; #等待1分钟左右再执行此句
============================================= mysql binglog export
mysqlbinlog -u root -p --start-datetime='2016-10-27 10:00:14' --stop-datetime='2016-10-27 13:02:14' /home/mysql/data/mysql-bin.000001 > /home/mysql/20161027-1.binlog
============================================= 单独恢复mysql某表
1.解压备份文件,得到完整备份文件sql
2.shell执行得到该表数据并保存到某个sql文件
grep 'INSERT INTO `tbl_001` VALUES' mydb20160606.sql > /home/mysql/dbbak/ex001.sql
3.mysql中清除数据
>mysql truncate table tbl_001;
# 如果遇到外键约束禁止truncate,当前会话中执行 set foreign_key_checks=0; 数据恢复完毕后记得当前会话恢复约束 set foreign_key_checks=1;
4.mysql中导入数据,完成数据恢复
>mysql source /home/mysql/dbbak/ex001.sql;
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。