安装
# 下载
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar
# 解压
tar -xvf mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar
# 强制安装
rpm -ivh *.rpm --nodeps --force
配置
通过快速配置脚本进行数据库的配置
- 初始化mysql和root用户,root用户不开放外网访问
- 新建非root用户,开放所有库和表权限,外网可访问
#!/bin/bash
# 备份配置文件
cur_time=`date +%Y%m%d.%H%m%s`
cp /etc/my.cnf /etc/my.cnf.${cur_time}.bak
# 临时关闭SELINUX
setenforce 0;
# ***********************************************************
# ****************** 初始化MySQL配置 *************************
# ***********************************************************
# 初始化mysql数据库实例信息
echo '初始化mysql数据库信息...';
echo '【提示】若输入为空,将读取默认值!';
# 修改MySQL数据库配置(按实际需求添加,切勿重复执行)
read -p "Are you need init mysql database? Defalut value is N. Y/N:" IS_NEED_INIT;
if [ "$IS_NEED_INIT" == 'Y' ]
then
read -p "Enter the Port of mysql,default is 3306:" MYSQL_PORT
if [ -z ${MYSQL_PORT} ]
then
echo 'your input is null!take the default value:3306';
MYSQL_PORT=3306;
else
echo ${MYSQL_PORT};
fi
read -p "Enter the Password of mysql user root,default is 123456:" MYSQL_ROOT_PWD;
if [ -z ${MYSQL_ROOT_PWD} ]
then
echo 'your input is null!take the default value:123456';
MYSQL_ROOT_PWD=123456;
else
echo ${MYSQL_ROOT_PWD};
fi
read -p "Enter the lower_case_table_names of mysql,default is 1:" MYSQL_LCTN;
if [ -z ${MYSQL_LCTN} ]
then
echo 'your input is null!take the default value:1';
MYSQL_LCTN=1;
else
echo ${MYSQL_LCTN};
fi
read -p "Enter the max_connections of mysql,default is 500:" MYSQL_MC;
if [ -z ${MYSQL_MC} ]
then
echo 'your input is null!take the default value:500';
MYSQL_MC=500;
else
echo ${MYSQL_MC};
fi
read -p "Enter the max_connect_errors of mysql,default is 20:" MYSQL_MCE;
if [ -z ${MYSQL_MCE} ]
then
echo 'your input is null!take the default value:20';
MYSQL_MCE=20;
else
echo ${MYSQL_MCE};
fi
read -p "Enter the default-time_zone of mysql,default is '+08:00':" MYSQL_DTZ;
if [ -z ${MYSQL_DTZ} ]
then
echo "your input is null!take the default value:'+08:00'";
MYSQL_DTZ='+08:00';
else
echo ${MYSQL_DTZ};
fi
echo -e "\n#端口\nport=${MYSQL_PORT}" >> /etc/my.cnf;
echo -e "\n#区分大小写\nlower_case_table_names=${MYSQL_LCTN}" >> /etc/my.cnf;
echo -e "\n#允许最大连接数\nmax_connections=${MYSQL_MC}" >> /etc/my.cnf
echo -e "\n#允许连接失败的次数\nmax_connect_errors=${MYSQL_MCE}" >> /etc/my.cnf
echo -e "\n#设置默认时区\ndefault-time_zone=${MYSQL_DTZ}" >> /etc/my.cnf
echo 'MySQL服务正在重启,请稍候...';
systemctl restart mysqld.service
# 等待5秒
sleep $[5]
systemctl status mysqld.service
# 修改root密码
# 获取初始默认密码
tmp=`grep 'temporary password' /var/log/mysqld.log`
oldPwd=${tmp##*': '}
# 修改root密码
mysql -uroot -p${oldPwd} -P${MYSQL_PORT} --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PWD}';flush privileges;"
echo 'MySQL服务root密码修改完成,初始密码为:['${oldPwd}'],新密码为:['${MYSQL_ROOT_PWD}']';
else
echo 'init passed!';
fi
# ***********************************************************
# ****************** 新建非root用户,拥有对所有库和表的增删改查操作 ********************
# ***********************************************************
echo '新建非root用户,拥有对所有库和表的增删改查操作...';
echo '【提示】若输入为空,将读取默认值!';
read -p "Enter the Port of mysql,default is 3306:" MYSQL_PORT
if [ -z ${MYSQL_PORT} ]
then
echo 'your input is null!take the default value:3306';
MYSQL_PORT=3306;
else
echo ${MYSQL_PORT};
fi
read -p "Enter the Password of mysql user root,default is 123456:" MYSQL_ROOT_PWD;
if [ -z ${MYSQL_ROOT_PWD} ]
then
echo 'your input is null!take the default value:123456';
MYSQL_ROOT_PWD=123456;
else
echo ${MYSQL_ROOT_PWD};
fi
read -p "Enter the Username of mysql,default is tom:" MYSQL_UN;
if [ -z "${MYSQL_UN}"]
then
echo 'your input is null!take the default value:tom';
MYSQL_UN=tom;
else
echo ${MYSQL_UN};
fi
read -p "Enter the Password of mysql user ${MYSQL_UN},default is 123456:" MYSQL_PWD;
if [ -z "${MYSQL_PWD}"]
then
echo 'your input is null!take the default value:123456';
MYSQL_PWD=123456;
else
echo ${MYSQL_PWD};
fi
echo '正在初始化${MYSQL_UN}用户...';
mysql -uroot -p${MYSQL_ROOT_PWD} -P${MYSQL_PORT} -e "
create user '${MYSQL_UN}'@'%' identified by '${MYSQL_PWD}';
grant all privileges on *.* to '${MYSQL_UN}'@'%';
flush privileges;"
echo '${MYSQL_UN}用户创建完毕...';