使用shell一键安装mysql
set -x
cat > mysql.sh << "EOF"
# 安装Mysql
#!/bin/bash
# 安装目录
INSTALL_DIR=/mysql
# 数据目录
DATA_DIR=/opt
# 数据库端口
MYSQL_PORT=4000
# 数据库密码
MYSQL_PASSWORD=root@.com
# Mysql下载地址(tar.gz结尾)
MYSQL_DOWNLOAD_URL=https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz;
function install_mysql(){
if [ ! -d "${INSTALL_DIR}" ]; then
mkdir -p ${INSTALL_DIR}
fi
yum install -y wget libaio && wget -P /tmp ${MYSQL_DOWNLOAD_URL} --no-check-certificate
tar -zxvf /tmp/mysql* -C ${INSTALL_DIR}/ && mv ${INSTALL_DIR}/mysql* ${INSTALL_DIR}/mysql;
cd ${INSTALL_DIR}/mysql;groupadd mysql;useradd -r -g mysql mysql;
chown mysql:mysql -R ${INSTALL_DIR}/mysql;
echo "
[mysqld]
bind-address=0.0.0.0 # 绑定地址运行远程连接
port=${MYSQL_PORT} # Mysql开放的端口
user=mysql # 数据库登录用户
basedir=${INSTALL_DIR}/mysql # Mysql安装的绝对路径
datadir=${DATA_DIR}/mysql # Mysql数据存放的绝对路径
socket=/tmp/mysql.sock # 套接字文件
log-error=${DATA_DIR}/mysql/mysql.err # mysql生成的错误日志存放的路径
pid-file=${DATA_DIR}/mysql/mysql.pid # 为mysqld程序指定一个存放进程ID的文件
character_set_server=utf8mb4 # 数据库字符编码
symbolic-links=0 # 是否开启链接符号
explicit_defaults_for_timestamp=true # 数据库timestamp类型的列自动更新
" > /etc/my.cnf
cd ${INSTALL_DIR}/mysql/bin/;
./mysqld --defaults-file=/etc/my.cnf --basedir=${INSTALL_DIR}/mysql/ --datadir=${DATA_DIR}/mysql/ --user=mysql --initialize;
echo "
[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=${DATA_DIR}/mysql/mysql.pid
ExecStart=${INSTALL_DIR}/mysql/support-files/mysql.server start
Restart=on-failure
PrivateTmp=false
[Install]
WantedBy=multi-user.target
" > /usr/lib/systemd/system/mysql.service
systemctl daemon-reload;systemctl start mysql;systemctl status mysql;
firewall-cmd --zone=public --add-port=${MYSQL_PORT}/tcp --permanent;firewall-cmd --reload;firewall-cmd --list-all;
rm -rf /usr/bin/mysql;ln -s ${INSTALL_DIR}/mysql/bin/mysql /usr/bin;
rm -rf /usr/bin/mysqldump;ln -s ${INSTALL_DIR}/mysql/bin/mysqldump /usr/bin;
PASSWORD=`cat ${DATA_DIR}/mysql/mysql.err | grep "temporary password"|awk -F"root@localhost: " '{print $2}'`;
mysql --connect-expired-password -uroot -p${PASSWORD} -D mysql -e "alter user 'root'@'localhost' identified by '$MYSQL_PASSWORD';flush privileges;update user set host = '%' where user = 'root';flush privileges;"
ln -s ${INSTALL_DIR}/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog;
ln -s ${INSTALL_DIR}/mysql/bin/mysql /usr/local/bin/mysql;
systemctl daemon-reload;systemctl restart mysql;systemctl enable mysql;systemctl status mysql;
echo "==========> MYSQL信息 <========== "
echo " 数据库密码 : ${MYSQL_PASSWORD} "
echo " 数据库端口 : ${MYSQL_PORT} "
echo " BASEDIR目录: ${INSTALL_DIR}/mysql "
echo " DATADIR目录: ${DATA_DIR}/mysql "
}
install_mysql
EOF
chmod +x mysql.sh && ./mysql.sh
本文含有隐藏内容,请 开通VIP 后查看