您好,登錄后才能下訂單哦!
上篇詳解了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。