應用系統分散式構建運維 1+x初級,項目四 部署主從資料庫 基礎環境安裝 準備兩台主機 修改主機名 # hostnamectl set-hostname mysql1 # hostnamectl set-hostname mysql2 關閉防火牆及SELinux服務(兩個節點) # setenfor ...
應用系統分散式構建運維
1+x初級,項目四
部署主從資料庫
基礎環境安裝
準備兩台主機
修改主機名
# hostnamectl set-hostname mysql1
# hostnamectl set-hostname mysql2
關閉防火牆及SELinux服務(兩個節點)
# setenforce 0
# systemctl stop firewalld
配置hosts文件(兩個節點)
# vi /etc/hosts
加入以下內容
192.168.37.16 mysql1
192.168.37.17 mysql2
安裝資料庫服務(兩個節點)
# yum install -y mariadb mariadb-server
啟動資料庫服務並設置開機自啟
# systemctl start mariadb
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
初始化資料庫並配置主從服務
初始化資料庫(兩個節點)
# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here. Enter current password for root (enter for none): ##預設按回車
OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation. Set root password? [Y/n] y
New password: ##輸入資料庫root密碼 Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment. Remove anonymous users? [Y/n] y
... Success! Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n
... skipping. By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment. Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success! Reloading the privilege tables will ensure that all changes made so far
will take effect immediately. Reload privilege tables now? [Y/n] y
... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB
installation should now be secure. Thanks for using MariaDB!
配置mysql1主節點
修改mysql1節點的資料庫配置文件
# vi /etc/my.cnf
[mysqld]
log_bin=mysql-bin ##記錄操作日誌
binlog_ignore_db=mysql ##不同步mysql系統資料庫
server_id=16 ##資料庫集群中的每個節點id都要不同
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
重啟資料庫服務
# systemctl restart mariadb
進入資料庫
# mysql -uroot -p123456
授權在任何客戶端機器上可以以root用戶登錄到資料庫
> grant all privileges on *.* to root@'%' identified by "123456";
在主節點上創建一個用戶連接節點mysql2,並賦予從節點同步主節點資料庫的許可權
> grant replication slave on *.* to 'user'@'mysql2' identified by '123456';
配置mysql2從節點
修改mysql2節點的資料庫配置文件
# vi /etc/my.cnf
[mysqld]
log_bin=mysql-bin ##記錄操作日誌
binlog_ignore_db=mysql ##不同步mysql系統資料庫
server_id=17 ##資料庫集群中的每個節點id都要不同
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
重啟資料庫服務
# systemctl restart mariadb
進入資料庫
# mysql -uroot -p123456
配置從節點連接主節點的連接信息
> change master to master_host='mysql1',master_user='user',master_password='123456';
開啟從節點服務
> start slave;
查看從節點服務狀態
> show slave status\G
配置資料庫主從集群成功
驗證資料庫主從服務
主節點創建資料庫
在主節點中創建庫
> create database test;
> use test;
在庫中創建表
> create table company(id int not null primary key,name varchar(50),addr varchar(255));
插入表數據
> insert into company values(1,"alibaba","china");
查看表數據
> select * from company;
從節點驗證複製功能
查看資料庫列表
> show databases;
> use test;
查詢表
> show tables;
查詢內容,驗證複製功能
> select * from company;
驗證從資料庫的複製功能成功
部署Nginx服務
基礎環境安裝
修改主機名
# hostnamectl set-hostname nginx
關閉防火牆及SELinux服務
# setenforce 0
# systemctl stop firewalld
安裝配置基礎服務
編譯安裝基礎環境
# yum install -y gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel
創建指定用戶
# groupadd -g 1001 nginx
# useradd -u 900 nginx -g nginx -s /sbin/nologin
# tail -1 /etc/passwd
nginx:x:900:1001::/home/nginx:/sbin/nologin
安裝配置nginx服務
將提供的nginx-1.12.2.tar.gz壓縮包上傳至/usr/local/src/目錄下,並解壓到當前目錄
# cd /usr/local/src
# tar -zxvf nginx-1.12.2.tar.gz
編譯並安裝
# cd nginx-1.12.2
# ./configure --prefix=/usr/local/nginx --with-http_dav_module \
> --with-http_stub_status_module --with-http_addition_module \
> --with-http_sub_module --with-http_flv_module --with-http_mp4_module \
> --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx
沒有報錯,進行安裝
# make && make install
創建軟鏈接
# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
啟動測試
# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# nginx
# netstat -ntpl
80埠啟動,表示nginx服務啟動成功
安裝PHP環境
基礎環境安裝
修改主機名
# hostnamectl set-hostname php
關閉防火牆及SELinux服務
# setenforce 0
# systemctl stop firewalld
安裝配置基礎服務
編譯安裝基礎環境
# yum install -y gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel
將提供的libmcrypt-2.5.8.tar.gz壓縮包上傳至/usr/local/src目錄下,並解壓到當前目錄
# cd /usr/local/src
# tar -zxvf libmcrypt-2.5.8.tar.gz
編譯安裝服務
# cd libmcrypt-2.5.8
# ./configure --prefix=/usr/local/libmcrypt && make && make install
安裝PHP環境
將提供的php-5.6.27.tar.gz壓縮包上傳至/usr/local/src目錄下,並解壓到當前目錄
# cd /usr/local/src/
# tar -zxvf php-5.6.27.tar.gz
編譯安裝服務
# cd php-5.6.27
# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm \
--enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir \
--with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash \
--with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
沒有報錯,進行安裝
# make && make install
創建用戶ID
這個nginx的id號要和nginx主機上的保持一致
# groupadd -g 1001 nginx
# useradd -u 900 nginx -g nginx -s /sbin/nologin
# tail -1 /etc/passwd
nginx:x:900:1001::/home/nginx:/sbin/nologin
配置PHP環境
PHP壓縮包中提供了PHP環境需要用到的模板文件,需要對文件進行改名後才能使用
複製文件並改名
# cp php.ini-production /etc/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
賦予文件執行許可權
# chmod +x /etc/init.d/php-fpm
添加PHP服務到啟動列表,並設置開機自啟
# chkconfig --add php-fpm
# chkconfig php-fpm on
修改PHP的主配置文件
# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
# grep -n '^'[a-Z] /usr/local/php5.6/etc/php-fpm.conf
149:user = nginx
150:group = nginx
164:listen = 192.168.37.13:9000
224:pm = dynamic
235:pm.max_children = 50
240:pm.start_servers = 5
245:pm.min_spare_servers = 5
250:pm.max_spare_servers = 35
啟動PHP服務
啟動PHP服務
# service php-fpm start
Starting php-fpm done
查看啟動狀態
# netstat -ntpl
9000埠啟動,表示PHP環境安裝完畢
分散式部署LNMP+WordPress
已經完成了主從資料庫的安裝配置、Nginx服務的安裝、PHP環境的安裝的四台機器進行部署
分散式LNMP環境的調試
配置Nginx服務支持PHP環境(nginx節點)
修改配置文件
# vi /usr/local/nginx/conf/nginx.conf
location / {
root /www; ##更改網頁目錄
index index.php index.html index.htm;
}
location ~ \.php$ { ##去掉這部分前面的註釋符
root /www; ##更改目錄
fastcgi_pass 192.168.37.13:9000; ##添加PHP主機IP地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
添加配置
# vi /usr/local/nginx/conf/fastcgi_params
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ##添加這行
fastcgi_param REQUEST_URI $request_uri;
創建目錄(nginx和php節點)
在nginx和php節點,創建/www目錄,並修改用戶和用戶組
# mkdir /www
# chown nginx:nginx /www/
部署WordPress(nginx和php節點)
將提供的wordpress-4.7.3-zh_CN.zip壓縮包上傳至nginx節點和php節點的/root目錄下並解壓
# yum install -y unzip
# unzip wordpress-4.7.3-zh_CN.zip
將解壓後的文件複製到/www目錄下
# mv wordpress/* /www/
修改wordpress的配置文件(nginx節點)
將模板文件複製並修改
# cp /www/wp-config-sample.php /www/wp-config.php
# vi /www/wp-config.php
// ** MySQL 設置 - 具體信息來自您正在使用的主機 ** ///** WordPress資料庫的名稱 */
define('DB_NAME', 'wordpress'); /** MySQL資料庫用戶名 */
define('DB_USER', 'root'); /** MySQL資料庫密碼 */
define('DB_PASSWORD', '123456'); /** MySQL主機 */
define('DB_HOST', '192.168.37.16'); ##此處IP為mysql1的地址 /** 創建數據表時預設的文字編碼 */
define('DB_CHARSET', 'utf8'); /** 資料庫整理類型。如不確定請勿更改 */
define('DB_COLLATE', ''); 將該配置文件複製到php節點(nginx節點) # scp /www/wp-config.php [email protected]:/www/
The authenticity of host '192.168.37.13 (192.168.37.13)' can't be established.
ECDSA key fingerprint is SHA256:C2d2Z+sCPaySJhwUjJ6I9fcmVW/rCBNL/7qI4lm8fd8.
ECDSA key fingerprint is MD5:84:3a:fb:c4:c1:15:b6:99:6f:62:f9:4b:46:a4:60:8c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.13' (ECDSA) to the list of known hosts.
[email protected]'s password: ##輸入PHP節點密碼
wp-config.php 100% 2909 1.9MB/s 00:00
創建WordPress資料庫(mysql1節點)
登錄資料庫
# mysql -uroot -p123456
創建資料庫
> create database wordpress;
驗證WordPress應用(nginx節點)
重啟nginx服務
# nginx -s reload
使用網頁訪問192.168.37.12(nginx節點ip)
填寫信息之後點擊左下角安裝
進入後臺界面
點擊左上角圖標
分散式部署完成