容器架構轉傳統lnmp架構(失敗篇)

撒拉黑呦發表於2020-12-24

因為一些特殊原因我要將此部落格的系統架構更換為傳統lnmp架構,這裡坐下記錄。

首先做下資料庫遷移,連線到容器內部檢視相應資料庫版本,匯出sql資料。
[root@xyz ~]# docker exec -it wordpress_db_1 /bin/bash
root@0f975d71dd50:/# mysql -p
mysql> select @@version;
退出容器,匯出sql語句
[root@xyz ~]# docker exec -it wordpress_db_1  mysqldump -uroot -pwordpress wordpress >/root/wp.sql
scp傳給遷入端伺服器
[root@xyz ~]# scp wp.sql 124.70.208.75:/root/

在遷移伺服器上查詢解除安裝系統自帶的關於mysql或mariadb的軟體包。

倆條執行沒有結果則可以安裝,有結果的話,請執行下面命令解除安裝。
[root@hecs-x-medium-2-linux-20200610093015 ~]# rpm -qa |grep mysql
[root@hecs-x-medium-2-linux-20200610093015 ~]# rpm -qa |grep mariadb
解除安裝命令
[root@hecs-x-medium-2-linux-20200610093015 ~]# yum -y remove mysql*
[root@hecs-x-medium-2-linux-20200610093015 ~]# yum -y remove mariadb*

查詢得出容器內的資料庫版本為mysql5.7.30,安裝對應版本資料庫。

[root@hecs-x-medium-2-linux-20200610093015 ~]# curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@hecs-x-medium-2-linux-20200610093015 ~]# tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@hecs-x-medium-2-linux-20200610093015 ~]# cd /usr/local
[root@hecs-x-medium-2-linux-20200610093015 local]# mv mysql-5.7.30-linux-glibc2.12-x86_64/ mysql

初始化資料庫並更改root密碼

[root@hecs-x-medium-2-linux-20200610093015 mysql]# useradd -r mysql -s /sbin/nologin
執行下面這條初始化命令報錯
[root@hecs-x-medium-2-linux-20200610093015 mysql]# ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

2020-07-03 15:46:15 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-07-03 15:46:15 [ERROR] Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
2020-07-03 15:46:15 [ERROR] Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql
-- server log begin --

-- server log end --

提示mysql_install_db 這條命令被棄用了,建議考慮 mysqld --initialize,好吧執行下面這條命令

[root@hecs-x-medium-2-linux-20200610093015 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

但是報錯

./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

不要慌,這個時候安裝libaio,安裝完畢重新執行上一條命令。

[root@hecs-x-medium-2-linux-20200610093015 mysql]# yum install libaio -y

[root@hecs-x-medium-2-linux-20200610093015 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

2020-07-03T07:53:44.394814Z 1 [Note] A temporary password is generated for root@localhost: Z;23cw-j2Qih

安裝完成,會提示上面這一行note,給出了初始root密碼。

[root@hecs-x-medium-2-linux-20200610093015 mysql]# cd support-files/

[root@hecs-x-medium-2-linux-20200610093015 support-files]# ./mysql.server start
Starting MySQL.Logging to '/usr/local/mysql/data/hecs-x-medium-2-linux-20200610093015.err'.
SUCCESS!
執行啟動命令,啟動資料庫。[root@hecs-x-medium-2-linux-20200610093015 support-files]# ../bin/mysql -p
輸入初始化密碼,可進入資料庫。進入資料庫需要設密碼,否則執行什麼都會報1820錯誤ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
這個時候執行mysql> set password = password('新密碼');
mysql> flush privileges;
退出資料庫,輸入新密碼就可以進入正常使用了。mysql> exit
[root@hecs-x-medium-2-linux-20200610093015 support-files]# /usr/local/mysql/bin/mysql -p

遷入端資料庫mysql5.7.30部署完畢。建立資料庫,執行匯入資料。

mysql> create database wordpress;
mysql> use wordpress;
mysql> source /root/wp.sql;

安裝nginx,並設定開機自啟動。

[root@hecs-x-medium-2-linux-20200610093015 support-files]# yum -y install nginx
[root@hecs-x-medium-2-linux-20200610093015 support-files]# systemctl start nginx
[root@hecs-x-medium-2-linux-20200610093015 support-files]# systemctl enable nginx

容器端匯出web家目錄中的檔案,放到遷入端伺服器nginx家目錄。

[root@xyz ~]# mkdir web
[root@xyz ~]# docker cp wordpress_wordpress_1:/var/www/html/. /root/web/
[root@xyz ~]# scp -r web/ 124.70.208.75:/root
這裡建議打個包傳輸,不然小檔案太多,很慢的。
[root@hecs-x-medium-2-linux-20200610093015 support-files]# rm -rf /usr/share/nginx/html
[root@hecs-x-medium-2-linux-20200610093015 support-files]# mv /root/web /usr/share/nginx/
[root@hecs-x-medium-2-linux-20200610093015 support-files]# mv /usr/share/nginx/web/ /usr/share/nginx/html

安裝配置php軟體

安裝php軟體依賴
[root@hecs-x-medium-2-linux-20200610093015 support-files]#  yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
[root@hecs-x-medium-2-linux-20200610093015 ~] # curl -O https://www.php.net/distributions/php-7.2.31.tar.gz
當前目錄下(複製這個主機名太長了,以下省略)
# tar -zxf php-7.2.31.tar.gz
# cd php-7.2.31
#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
配置完成編譯安裝
# make && make install
安裝完成後,複製配置檔案到對應目錄
# cp /root/php-7.2.31/php.ini-development /usr/local/php/etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
新增啟動服務
# cp /root/php-7.2.31/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# useradd -r www -s /sbin/nologin
# service php-fpm start
把php下的bin目錄新增到環境變數
# echo 'export PATH=$PATH:/usr/local/php/bin' >> /etc/profile
# source /etc/profile

更改配置檔案讓nginx找到php解析器。

# vim /etc/nginx/nginx.conf
將以下內容新增到對應server區塊中
location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
# nginx -t 檢查下語法是否有誤。
# systemctl restart nginx

最後,不好意思失敗了,提示資料庫連線失敗,我TM(一萬個字小聲逼逼。。。)?總體搭建nginx環境沒問題,是程式碼和資料庫連線這裡出了問題,等想好再回來改。做個記錄,這個mysql5.7.30是真的坑,實驗好幾次都沒搭成功,不過最後總算ok了。

這篇就沒臉要點贊轉發了。有特殊見解的歡迎騷擾。再見。

相關文章