Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

准备工作

MySQL 该版本下载地址:

https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

mysql_install-v1.0.sh脚本如下:

#!/bin/bash

# MySQL 安装日志信息
log=/var/log/mysql_install.log
# MySQL压缩包路径
MySQLPath=/root/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
# sql 文件
sqlfile=/root/test.sql

# 卸载系统?带的MARIADB
rpm -qa|grep mariadb |xargs yum remove -y > /dev/null

# 解压MYSQL安装包
echo ‘====================================‘
echo ‘Unzip....‘
tar -zxf $MySQLPath -C /usr/local/
mv /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql
if [ $? -ne 0 ];then
echo ‘MySQL unzip failed!!!‘
exit
else
echo ‘====================================‘
echo ‘Unpack the success!‘
fi

# 创建MYSQL?户和?户组
groupadd mysql
useradd -g mysql mysql
mkdir /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
echo ‘====================================‘
echo ‘User created successfully!‘

# MYSQL的配置?件
cat > /etc/my.cnf << EOF
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
port = 3306
socket=/var/lib/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
EOF
if [ $? -ne 0 ];then
echo ‘Failed to write the my.cnf configuration file!!!‘
exit
else
echo ‘====================================‘
echo ‘my.cnf configuration success!‘
echo ‘====================================‘
fi

# 创建 /var/lib/mysql ?录,并修改权限
mkdir /var/lib/mysql
chmod 777 /var/lib/mysql

# 开始安装 MYSQL
cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2>&1 | tee $log

# 获取 root 初始密码
InitialPassword=`tail -1 $log |awk ‘{print $NF}‘`

# 复制启动脚本到资源?录,并修改 basedir 和 datadir 为对应?录
cp ./support-files/mysql.server /etc/init.d/mysqld
sed -i ‘46s/basedir=/basedir=\/usr\/local\/mysql/g‘ /etc/init.d/mysqld
sed -i ‘47s/datadir=/datadir=\/usr\/local\/mysql\/data/g‘ /etc/init.d/mysqld
if [ $? -ne 0 ];then
echo ‘Failed to replace /etc/init.d/mysqld!!!‘
exit
else
echo ‘====================================‘
echo ‘/etc/init.d/mysqld successfully modified!‘
echo ‘====================================‘
fi

# 设置MYSQL系统服务并开启?启
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

# 启动MYSQLD
service mysqld restart

# 检查 MySQL 服务是否启动成功
netstat -ntlp | grep 3306
if [ $? -ne 0 ];then
echo ‘MySQL Boot failure!!!‘
exit
else
echo ‘====================================‘
echo ‘start MySQL successfully!‘
fi

# 将 MYSQL 的 BIN ?录加? PATH 环境变量
echo "export PATH=\$PATH:/usr/local/mysql/bin" >> ~/.bash_profile
source ~/.bash_profile

if [ $? -ne 0 ];then
echo ‘Variable write failed!!!‘
exit
else
echo ‘====================================‘
echo ‘Variable write successful!‘
echo ‘====================================‘
fi

# 登陆MYSQL,并执行 SQL 语句
mysql -uroot -p"$InitialPassword" --connect-expired-password < $sqlfile 2> /dev/null

if [ $? -ne 0 ];then
echo ‘fail to perform MySQL operation!!!‘
exit
else
echo ‘====================================‘
echo ‘All operations were successful and can be connected through Navicat!!!‘
echo ‘====================================‘
fi

test.sql文件内容如下:

alter user user() identified by "123456";
use mysql;
update user set user.Host=‘%‘ where user.User=‘root‘;
flush privileges;

第一、修改 root 密码;

第二、设置远程主机登录,可以通过Navicat来登录。

执行脚本

这里需要注意的是不要使用 ./mysql_install-v1.0.sh方式来执行,否则系统环境变量执行完脚本之后不生效;

需要使用source mysql_install-v1.0.sh来执行脚本才可以。

Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

使用修改后的密码登录测试

Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

Navicat 连接测试

Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

后续还会继续更新其他应用一键安装脚本,欢迎关注我哦!!!

Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

上一篇:数据库索引灵魂拷问(2)


下一篇:windows下开启mysql日志