一键安装脚本内容 {#一键安装脚本内容}
#!/bin/bash
# https://blog.csdn.net/qq_41054313
#数据库密码
mysqlPWD="lhDream@123"
echo "--MySQL5.7安装--"
echo "下载依赖环境"
yum -y install wget
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
echo "开始安装"
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
echo "启动MySQL"
systemctl start mysqld.service
systemctl status mysqld.service
a=$(systemctl status mysqld.service \| grep "active (running)" \| wc -l)
if \[ $a -gt 0 \]
then
echo "启动完成,状态正常"
#配置MySQL
echo "配置MySQL"
str=$(grep <span class="hljs-string">"password is generated for root@localhost:"</span> /var/log/mysqld.log)
localPWD=<span class="hljs-variable">${str##*"root@localhost: "}</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"数据库默认密码:"</span><span class="hljs-variable">$localPWD</span>
<span class="hljs-built_in">export</span> MYSQL_PWD=<span class="hljs-variable">$localPWD</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"重置数据库密码为:"</span><span class="hljs-variable">$mysqlPWD</span>
mysql --connect-expired-password -uroot -e <span class="hljs-string">"ALTER USER 'root'@'localhost' IDENTIFIED BY '<span class="hljs-variable">$mysqlPWD</span>'"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"刷新权限"</span>
<span class="hljs-built_in">export</span> MYSQL_PWD=<span class="hljs-variable">$mysqlPWD</span>
mysql --connect-expired-password -uroot -e <span class="hljs-string">"flush privileges"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"配置远程登录"</span>
mysql --connect-expired-password -uroot -e <span class="hljs-string">"grant all privileges on *.* to 'root'@'%' identified by '<span class="hljs-variable">$mysqlPWD</span>' with grant option"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"配置数据库编码"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"[client]"</span> > /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"default-character-set=utf8"</span> >> /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">""</span> >> /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"[mysqld]"</span> >> /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"datadir=/var/lib/mysql"</span> >> /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"socket=/var/lib/mysql/mysql.sock"</span> >> /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"character-set-server=utf8"</span> >> /etc/my.cnf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"collation-server=utf8_general_ci"</span> >> /etc/my.cnf
<span class="hljs-built_in">cat</span> /etc/my.cnf
<span class="hljs-comment">#重启MySQL查看配置结果</span>
systemctl restart mysqld
systemctl status mysqld.service
mysql --connect-expired-password -uroot -e <span class="hljs-string">"status"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"安装完成"</span>
else`
`echo` `"状态异常,安装失败"`
`fi`
`#rm -f mysql57-community-release-el7-10.noarch.rpm`
`echo` `"exit"`
`
可直接使用命令 {#可直接使用命令}
bash <(curl -s -S -L https://lhdream.github.io/LinuxBash/installMySQL.sh)