溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)

發(fā)布時(shí)間:2020-06-27 10:23:06 來源:網(wǎng)絡(luò) 閱讀:351 作者:vmzhang 欄目:系統(tǒng)運(yùn)維

1、基于源碼編譯安裝的LNP+MYSQL主從實(shí)戰(zhàn)
準(zhǔn)備3臺(tái)服務(wù)器,其中一臺(tái)作為Nginx WEB服務(wù)器+PHP-FPM(FastCGI),另外兩臺(tái)作為MYSQL主從服務(wù)器,服務(wù)器IP信息:
? 10.10.10.4-Linux+Nginx+PHP
? 10.10.10.5-MYSQL Master;
? 10.10.10.6-MYSQL Slave;
? 1) Nginx安裝配置
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
? #安裝PCRE庫和基礎(chǔ)庫支持
? yum install pcre-devel pcre gcc-c++ openssl openssl-devel zlib-devel -y
? cd /usr/src
? #下載Nginx源碼包
? wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
? tar -xzf nginx-1.12.0.tar.gz
? cd nginx-1.12.0
? #進(jìn)入解壓目錄,然后sed修改Nginx版本信息為JWS
? sed -i -e 's/1.12.0//g' -e 's/nginx\//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h
? #預(yù)編譯Nginx
? useradd www ;./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
? #編譯成功后,執(zhí)行make命令進(jìn)行編譯
? make
? #make 執(zhí)行成功后,執(zhí)行make install正式安裝
? make install
? #檢查nginx配置文件是否正確,返回OK即正確。
? /usr/local/nginx/sbin/nginx -t
? #回車即可。查看進(jìn)程是否已啟動(dòng):
? 然后啟動(dòng)nginx,/usr/local/nginx/sbin/nginx
? 2) php安裝配置
? wget http://museum.php.net/php5/php-5.6.9.tar.gz
? yum -y install gd curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql-devel
? tar -xzf php-5.6.9.tar.gz
? cd php-5.6.9
? ./configure --prefix=/usr/local/php5 --enable-fpm --enable-debug --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring --with-curl --with-mysql=mysqlnd
? --with-mysqli=mysqlnd
? --with-pdo-mysql=mysqlnd
? --disable-fileinfo
mysqli擴(kuò)展有一系列的優(yōu)勢,相對于mysql擴(kuò)展的提升主要有:面向?qū)ο蠼涌凇?prepared語句支持、多語句執(zhí)行支持、事務(wù)支持、增強(qiáng)的調(diào)試能力、嵌入式服務(wù)支持。
make
make install
\cp php.ini-development /usr/local/php5/lib/php.ini
\cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
\cp /usr/src/php-5.6.9/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
php解析器:用來解析PHP頁面
php-fpm管理器:用來管理和調(diào)用和開啟PHP解析器。

php-fpm.conf是php服務(wù)程序重要的配置文件之一,我們需要啟用該配置文件中第25行左右的pid文件保存目錄,然后分別將第148和149行的user與group參數(shù)分別修改為www賬戶和用戶組名稱:

vim /usr/local/php5/etc/php-fpm.conf

1 ;;;;;;;;;;;;;;;;;;;;;
2 ; FPM Configuration ;
3 ;;;;;;;;;;;;;;;;;;;;;
23 ; Note: the default prefix is /usr/local/php/var
24 ; Default Value: none
25 pid = run/php-fpm.pid
………………省略部分輸出信息………………
145 ; Unix user/group of processes
146 ; Note: The user is mandatory. If the group is not set, the default user's g roup
147 ; will be used.
148 user = www
149 group = www
………………省略部分輸出信息………………
(4) Nginx配置文件配置
cat /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/nginx/html;
index index.html index.php;
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;#
fastcgi_paramSCRIPT_FILENAME

s/usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
。

cat >/usr/local/nginx/html/index.php<<EOF
<?php
phpinfo();
?>
EOF
service php-fpm restart #重啟php服務(wù)
/usr/local/nginx/sbin/nginx –t 檢測Nginx服務(wù)參數(shù)
/usr/local/nginx/sbin/nginx 啟動(dòng)nginx服務(wù)參數(shù)
測試LNMP架構(gòu)測試,創(chuàng)建index.php測試頁面,如下圖所示:
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)

建立虛擬主機(jī)dz.jf.com && wp.jf.com
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)

-
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
server {
listen 80;
server_name dz.jf.com;

  location / {
     root /usr/local/nginx/html/dz;
     index  index.php;
     }
  error_page 500 502 503 504 /50x.html; 
  location = /50x.html {
  root html;
     }
   location ~ \.php$ {
         root           /usr/local/nginx/html/dz;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
               fastcgi_param SCRIPT_FILENAME  /usr/local/nginx/html/dz$fastcgi_script_name;
         include        fastcgi_params;
     }

} #兩個(gè)文件內(nèi)容中就是域名和路徑不一樣,其余都一樣
2-1)Discuz PHP論壇安裝
LAMP源碼整合完畢之后,Dicuz官網(wǎng)下載Discuz開源PHP軟件包,將軟件包解壓并發(fā)布在Nginx Htdocs發(fā)布目錄,代碼如下:
cd /usr/src ;
wget http://download.comsenz.com/DiscuzX/3.1/Discuz_X3.1_SC_UTF8.zip
unzip Discuz_X3.1_SC_UTF8.zip -d /usr/local/nginx/html/dz
cd /usr/local/nginx/html/dz
mv upload/ /usr/local/nginx/html/dz
chmod 757 -R data/ uc_server/ config/ uc_client/
重新啟動(dòng)nginx
另外那個(gè)一樣的操作,不同的路徑
2-2)wordpress PHP論壇安裝
cd /usr/src ;
tar –zxf wordpress-4.9.4-zh_CN.tar.gz –C /usr/local/nginx/html/wp
cd /usr/local/nginx/html/wp
mv wordpress /
.

如下圖找到路徑添加ip 與域名 ,這樣用主機(jī)就可以測試和訪問這兩個(gè)網(wǎng)站
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
MYSQL數(shù)據(jù)庫命令行中創(chuàng)建PHP連接MYSQL的用戶及密碼,命令如下:
create database discuz charset=utf8;
grant all on discuz. to root@'10.0.0.4' identified by "123456";
flush privileges;
create database wordpress charset=utf8;
grant all on wordpress.
to root@'10.0.0.4' identified by "123456";
flush privileges;

訪問IP地址 進(jìn)行論壇的訪問,配置discuz論壇設(shè)置數(shù)據(jù)庫。

? 3)MySQL安裝配置
1).安裝相關(guān)工具和軟件包
yum install cmake ncurses-devel ncurses libaio bison git gcc-c++ -y
cd /usr/src
wget /mysql-5.5.20.tar.gz
tar -xzf mysql-5.5.20.tar.gz
cd mysql-5-5.20
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql55 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0 \
-DENABLE_DTRACE=0

make -j4 && make install
cd /usr/local/mysql55/
\cp -f support-files/my-large.cnf /etc/my.cnf
\cp -f support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on
mkdir -p /data/mysql
useradd mysql
/usr/local/mysql55/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql55/
ln -s /usr/local/mysql55/bin/* /usr/bin/
service mysqld start
建立主從復(fù)制
2).
Master and slave 一樣 在/etc/my.conf 里面分別設(shè)置級別1,2;
設(shè)置日志名稱及id :
server-id = 1
log-bin=mysql-bin

master 上面創(chuàng)建slave 鏈接master的復(fù)制賬號和授權(quán)命令
Master數(shù)據(jù)庫服務(wù)器命令行中 創(chuàng)建tongbu用戶及密碼并設(shè)置權(quán)限,執(zhí)行如下命令,查看bin-log文件及position點(diǎn),
grant replication slave on . to 'tongbu'@'10.0.0.4' identified by'123456';
show master status;
flush tables with read lock; #主庫配置鎖表

提前關(guān)閉master 或者 slave 的防火墻,或者放開mysql端口,關(guān)閉selinux

3)
.slave 鏈接 master,請求bin-log 文件(mysql-bin)。
change master to
master_host='10.0.0.5',master_user='tongbu',master_password='123456',masterlog
file='mysql-bin.000003',master_log_pos=477;
slave start;
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.6
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 477
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 477
Relay_Log_Space: 825
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)

當(dāng)前如果io 進(jìn)程和 sql 進(jìn)程都是running ,就代表 主從同步就建立成功了。

4).
在主庫上面創(chuàng)建 數(shù)據(jù)庫和表 ,主庫和從庫之間就會(huì)形成主從同步關(guān)系。
master 測試:
unlock tables;
create database jf1;
查看從庫
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)


5).
主主同步就是用slave 的master去同步 mastar 的slave,叫做主從互備,一臺(tái)宕機(jī)后,只需在/etc/hosts 文件中改變ip地址就可以將其切換(前提是主主mysql數(shù)據(jù)庫主機(jī)名可以ping通和一致)
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
6).
模擬主宕機(jī)切換ip
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)10.0.0.5的ip
創(chuàng)建jf001和jf002用戶:

基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
Kill “mysql’s id”
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
切換另一個(gè)主庫(在安裝論壇和網(wǎng)站時(shí)一定要用域名,不然主宕機(jī)后無法切換)

基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)
基于源碼編譯和yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)

2、基于yum安裝的LNP+MYSQL主從實(shí)戰(zhàn)

配置LNP,Nginx WEB服務(wù)器+PHP-FPM(FastCGI),配置方法如下:
安裝LNP服務(wù);
yum install nginx php php-devel php-mysql php-fpm -y
Nginx默認(rèn)發(fā)布目錄:/usr/share/nginx/html/;
Nginx配置文件目錄:/etc/nginx/
/usr/share/nginx/html/,發(fā)布目錄創(chuàng)建index.php測試頁面;
啟動(dòng)Nginx、PHP-FPM服務(wù)命令;
service php-fpm restart
service nginx restart
(配置內(nèi)容和編譯安裝相似,唯一不同的是路徑和啟動(dòng)命令)
2) 配置MYSQL 主主同步
yum ?。椋睿螅簦幔欤臁。恚幔颍椋幔洌狻。恚幔颍椋幔洌猓洌澹觯澹臁。?br/>接下來的配置基本同編譯安裝mysql-5.5.20相似

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI