shell 脚本二进制安装mysql
以下脚本的手动安装连接:https://www.cnblogs.com/leihongnu/p/12581793.html
【
#/bin/bash
#脚本安装 mysql,上传安装包至 /root
cd /root
#安装日志
mysql_log=/root/mysql.log
# mysql 安装包名
mysql_package=mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
# 解压 mysql 安装包后文件夹名称
mysql_package_dir=mysql-8.0.19-linux-glibc2.12-x86_64
#存放数据库的目录
mysql_dbdata=/usr/local/mysql/data
#centos6 检查 mysql
if [ `rpm -qa | grep -i mysql | wc -l ` -ge 1 ]
then
rpm -qa | grep -i mysql >> uninstall_mysql.txt
file=$(cat uninstall_mysql.txt)
for name in $file
do
rpm -e --nodeps $name
done
fi
rm -rf uninstall_mysql.txt
#centos7 检查 mariadb
if [ `rpm -qa | grep -i mariadb | wc -l ` -ge 1 ]
then
rpm -qa | grep -i mariadb >> uninstall_mariadb.txt
file=$(cat uninstall_mariadb.txt)
for name in $file
do
rpm -e --nodeps $name
done
fi
rm -rf uninstall_mariadb.txt
#添加 mysql 用户
useradd -r -s /sbin/nologin mysql
#解压 mysql_package 至 /usr/local
tar -xvf $mysql_package -C /usr/local/
cd /usr/local
#创建软连接
ln -s $mysql_package_dir mysql
#创建 mysql 数据存放目录
mkdir $mysql_dbdata
#修改文件夹所属者
chown -R mysql:mysql mysql
chown -R mysql:mysql mysql/
#添加环境变量路径,使生效
echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
#生成配置文件
if [ -e /etc/my.cnf ]
then
mv /etc/my.cnf /etc/my.cnf.bak
rm -rf /etc/my.cnf
fi
touch /etc/my.cnf
echo "[client]" >> /etc/my.cnf
echo "port = 3306" >> /etc/my.cnf
echo "default-character-set=utf8" >> /etc/my.cnf
echo "[mysqld]" >> /etc/my.cnf
echo "character_set_server=utf8" >> /etc/my.cnf
echo "init_connect='SET NAMES utf8'" >> /etc/my.cnf
echo "basedir=/usr/local/mysql" >> /etc/my.cnf
echo "datadir=$mysql_dbdata" >> /etc/my.cnf
echo "socket=/tmp/mysql.sock" >> /etc/my.cnf
echo "#登录密码报错,取消屏蔽后重启可不输入密码进入mysql" >> /etc/my.cnf
echo "#skip-grant-tables" >> /etc/my.cnf
echo "#设置不区分大小写,默认是0,区分大小写" >> /etc/my.cnf
echo "#lower_case_table_names = 1" >> /etc/my.cnf
echo "[mysqld_safe]" >> /etc/my.cnf
echo "log-error=/var/log/mysqld.log" >> /etc/my.cnf
echo "pid-file=/var/run/mysqld/mysqld.pid" >> /etc/my.cnf
#生成数据库,默认生成密码打印在屏幕上,登录时会用到,记得保存
cd /usr/local/mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=$mysql_dbdata
#大小写设置要注意:mysql8之后对大小写敏感的需要在初始化的时候就设置,并且 my.cnf文件下的[mysqld]也要同步设置lower-case-table-names=1
#bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=$mysql_dbdata --initialize --lower-case-table-names=1
#创建RSA private key
bin/mysql_ssl_rsa_setup --datadir=$mysql_dbdata
#设置启动服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#添加启动服务
chkconfig --add mysqld
#启动服务
service mysqld start
#查看服务
systemctl status mysqld >> $mysql_log
】
过程截图:
中间省略
查看mysqld状态:
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';