第1章 搭建博客与数据库迁移
1.1 搭建博客
在LNMP环境搭建好的前提下,搭建博客。
1.1.1 确认环境,nginx、mysql和php服务在开启状态
[root@web02 ~]$ netstat -lntup|egrep "80|3306|9000"
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 12214/php-fpm: mast
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12420/nginx: master
tcp6 0 0 :::3306 :::* LISTEN 7285/mysqld
1.1.2 上传本地下载好的开源BLOG
[root@web02 ~]$ cd /server/tools/
[root@web02 /server/tools]$ rz -y <===上传本地压缩包
rz waiting to receive.
[root@web02 /server/tools]$ ls
wordpress-5.1.1.zip
[root@web02 /server/tools]$ unzip wordpress-5.1.1.zip <===解压缩
[root@web02 /server/tools]# mv wordpress/* /application/nginx/html/blog/
[root@web02 /server/tools]$ chown -R nginx.nginx /application/nginx/html/blog/
[root@web02 /server/tools]$ ls -ld /application/nginx/html/blog/
drwxr-xr-x 5 nginx nginx 4096 5月 7 09:14 /application/nginx/html/blog/
1.1.3 修改配置文件,内容如下
[root@web02 /application/nginx/conf/extra]$ cat 03_blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.php index.html index.htm;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ \.php$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
1.1.4 安装博客
打开浏览器,在地址栏输入blog.etiantian.org开始安装博客
1.2 在db01上安装数据库
useradd mysql -s /sbin/nologin -M
id mysql
mkdir -p /server/tools/
cd /server/tools/
#上传本地的mysql压缩包
rz
#解压安装
tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
mkdir -p /application
mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26
ln -s /application/mysql-5.7.26/ /application/mysql
#配置配置文件
rpm -e --nodeps mariadb-libs
cat >/etc/my.cnf<<EOF
[mysqld]
basedir = /application/mysql/
datadir = /application/mysql/data
socket = /tmp/mysql.sock
server_id = 1
port = 3306
log_error = /application/mysql/data/oldboy_mysql.err
[mysql]
socket = /tmp/mysql.sock
prompt = oldboy [\\d]>
EOF
#5.初始化数据库
rpm -qa mariadb-libs
yum install libaio-devel -y
mkdir -p /application/mysql/data
chown -R mysql.mysql /application/mysql/
/application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data
#6、配置启动
cat >/etc/systemd/system/mysqld.service<<EOF
[Unit]
Description=MySQL Server by oldboy
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
systemctl start mysqld
systemctl enable mysqld
netstat -lntup|grep mysql
#7.登录测试
echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
. /etc/profile
mysql
quit
1.3 数据库迁移
从单机LNMP迁移到db01独立的mysql
1.3.1 在web服务器上备份数据库
[root@web02 ~]$ mysqldump -uroot -poldboy123 -A -B|gzip >/tmp/web02_db.sql.gz
mysqldump: [Warning] Using a password on the command line interface can be insecure.
1.3.2 在web上将数据库拷贝到db01
[root@web02 ~]$ scp /tmp/web02_db.sql.gz 10.0.0.51:/tmp
[root@web02 ~]$ systemctl stop mysqld
[root@web02 ~]$ systemctl disable mysqld
[root@web02 ~]$ lsof -i :3306
1.3.3 在web上修改配置文件部分内容
[root@web02 ~]$ cd /application/nginx/html/blog/
[root@web02 /application/nginx/html/blog]$ vim wp-config.php
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'wordpress' );
/** MySQL database password */
define( 'DB_PASSWORD', 'oldboy123' );
/** MySQL hostname */
define( 'DB_HOST', '172.16.1.51' );
1.3.4 在db01上操作
[root@db01 ~]$ lsof -i :3306
[root@db01 ~]$ cd /tmp
[root@db01 /tmp]$ gzip -d web02_db.sql.gz <===解压压缩包
[root@db01 /tmp]$ mysql -uroot -poldboy123 </tmp/web02_db.sql <===导入数据库
1.3.5 在db01上登录数据库操作
[root@db01 /tmp]# mysql -uroot -poldboy123
oldboy [(none)]>grant all privileges on wordpress.* to wordpress@'172.16.1.%' identified by 'oldboy123';
oldboy [(none)]>flush privileges;
oldboy [(none)]>select user,authentication_string,host from mysql.user;