企业服务器LNMP环境搭建:
常见的软件架构:
1)C/S: client/server
2)B/S: browser/server
不管是C还是B,都是属于客户端属于前端。那么运维人员主要是负责和管理的Server端,也统称为服务器端。为了快速的提供服务器端使用,互联网中有多种解决方案和架构。其中以下两种架构有着广泛的使用基础。
项目采用的是PHP语言开发的,我们有两种架构进行选择。
LAMP:Linux+Apache+MySQL+PHP
LNMP:LInux+Apache+MySQL+PHP
LNMPA:Linux(静态页面交给Linux处理。)+Nginx+MySQL+PHP+Apache(动态页面。)
(现在不常见。)
LNMT:Linux+Nginx+MySQL+Tomcat(JDK JAVA)。
Nginx+MySQL+(Python+nodejs|ruby)
DB-Engines Ranking - popularity ranking of database management systems
在企业中搭建实际业务环境时,一般依赖我文件(小文件)使用yum安装解决,生产业务环境需要使用源码编译的方式进行安装。
源码编译安装(定制化安装)。
在使用脚本安装mysql软件之前。我们需要安装开发工具。
yum groups install Development Tools
#!/bin/bash
#源码编译安装MySQL
mysql_install() {
#1、创建用户
`id mysql` &>/dev/null
[ $? -ne 0 ] && useradd -s /sbin/nologin -M mysql
#2、解决依赖
yum install -y cmake
yum install -y ncurses-devel
#3、编译安装
cd /root/soft
tar zxvf mysql-5.6.33.tar.gz
cd mysql-5.6.33
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci\
-DWITH_SSL=bundled
make && make install
#配置文件
rm -rf /etc/my.cnf
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
#授权并初始化数据库
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#配置服务、自启动和环境变量
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
service mysqld start
chkconfig --add mysqld
echo 'PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
#删除匿名用户
#设置root域名的密码
rpm -qa|grep expect
if [ $? -ne 0 ];then
yum -y install expect
fi
#导入环境变量PATH
export PATH=/usr/local/mysql/bin:$PATH
#初始化root密码 删除匿名用户
echo '#!/usr/bin/expect
set timeout 60
spawn mysql_secure_installation
expect {
"enter for none" { send "\r"; exp_continue}
"Y/n" { send "Y\r" ; exp_continue}
"password" { send "123456\r"; exp_continue}
"Cleaning up" { send "\r"}
}
interact ' > mysql_secure_installation.exp
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
mysql_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'
出现了mysql:command not found的情况。
使用软链接的方式进行了解决:
ln -s /usr/local/mysql/bin/mysql /usr/bin
另外,再转一个mysql的脚本:
#/bin/bash
echo "" && read -p "请输入安装包路径:" src_path
for i in `seq 2`
do
if [ ! -f $src_path ];then
echo ""
read -p "文件不存在,请重新输入:" src_path
fi
if [ $i -eq 2 ] && [ ! -f $src_path ] ;then
echo -e "\n错误:请确认安装包路径后重新执行安装脚本。\n"
exit 1
fi
done
echo "" && read -p "请输入mysql安装路径:" base_path
if [ ! -d $base_path ];then
echo -e "\n错误:$base_path 不是一个目录,请确认后重新执行安装脚本。\n"
exit 1
fi
if [ -d $base_path ] && [ "`ls -A $base_path`" != "" ];then
echo -e "\n错误:$base_path 不是一个空的目录,请确认后重新执行安装脚本。\n"
exit 1
fi
echo -e "\n开始安装,请稍等..."
tar xf $src_path
pack_name=$(echo $src_path | awk -F '/' '{print $NF}')
mv ${pack_name%%.tar*}/* ${base_path}/
useradd mysql &> /dev/null
chown -R mysql.mysql $base_path
#生成my.cnf
cat > /etc/my.cnf << EOF
[mysqld]
user=mysql
basedir=$base_path
datadir=${base_path}/data
port=3306
character_set_server=utf8mb4
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
init_connect='SET collation_connection=utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
[mysql]
default-character-set=utf8mb4
[client]
port=3306
default-character-set=utf8mb4
EOF
#初始化安装
cd ${base_path}/bin && ./mysqld --initialize
if [ $? -ne 0 ];then
echo -e "\n错误:安装异常,请检查系统环境。\n"
exit 1
fi
cd ../ && cp -a ./support-files/mysql.server /etc/init.d/mysql && chmod +x /etc/init.d/mysql && chkconfig --add mysql
echo -e "\n正在启动 mysql..."
sleep 3
service mysql start
if [ $? -ne 0 ];then
echo -e "\nmysql 启动失败,请检查日志。后续请手动启动服务并修改初始化密码。"
exit 1
fi
cp /etc/profile{,-bak} && echo -e "\n\n#MYSQL\nMYSQL_HOME=$base_path\nPATH=\$PATH:\$MYSQL_HOME/bin" >> /etc/profile
source /etc/profile
echo "" && read -p "请输入初始化密码:" init_pwd
echo "" && read -p "请输入新密码:" new_pwd
sql(){
mysql -uroot -p$init_pwd --connect-expired-password << EOF
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$new_pwd';
flush privileges;
EOF
}
sql &> /dev/null
if [ $? -eq 0 ];then
echo -e "\n密码修改成功。\nroot@localhost 连接新密码为:$new_pwd"
else
echo -e "\n密码修改失败,请后续手动修改"
fi
echo -e "\nmysql安装启动完成,请输入 'mysql -uroot -p' ,键入密码连接数据库。"
[root@server01 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.33 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql>