首页 > 系统 > CentOS > 正文

CentOS7部署LNMP+Mongodb环境

2024-06-28 16:03:02
字体:
来源:转载
供稿:网友

上篇详解了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#改默认密码sqlalter user 'root'@'localhost' identified by 'sa'; #允许root远程登录,改密码为'sa'的sqlGRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'sa' WITH GRANT OPTION;flush privileges;#禁止root远程登录的sqldelete 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 makemake installvi /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”#启动MongoDBmongo  #进入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 * -frmkdir indexdelta# 如果sphinx服务无法启动或者启动有错误,可尝试重命名 mv /usr/local/sphinx/var/data/binlog.meta文件,再重启则ok============================================== jreyum install java-1.8.0-openjdk-develvi /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=100binlog-format=mixedlog-bin=mysql-binmax_binlog_size=1000Mbinlog-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=200binlog-format=mixedlog-bin=mysql-binmax_binlog_size=1000Mbinlog-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----slavescp root@192.168.1.100:/home/mysql/mydb20160606.sql.gz /home/mysql/mydb20160606.sql.gzgzip -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 apacheGroup apache#运行/usr/sbin/httpd#配置/etc/httpd/conf/httpd.conf#网站根/var/www/html#systemctl服务/usr/lib/systemd/system/httpd.servicesystemctl enable httpd.service #安装vsftpdyum -y install vsftpdvi /etc/vsftpd/vsftpd.conflocal_root = /var/www/htmluseradd myftp -s /sbin/nologin -d /var/www/html -g ftppasswd myftp    #设ftp密码chown -R myftp /var/www/htmlchmod -R 777 /var/www/htmlsystemctl 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 ACCEPTvi /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 exportmysqlbinlog -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.解压备份文件,得到完整备份文件sql2.shell执行得到该表数据并保存到某个sql文件grep 'INSERT INTO `tbl_001` VALUES' mydb20160606.sql > /home/mysql/dbbak/ex001.sql3.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;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表