mysql5.7安装脚本
#!/bin/bash
# 删除/etc/my.cnf文件
[ -f /etc/my.cnf ] && {
yum remove mariadb* -y &> /dev/null
}
# 安装库
yum search libaio &> /dev/null
yum install libaio -y &> /dev/null
echo "libaio is installd successfully"
# 定义路径,添加用户,组等
tar_path=/root #tar包路径
mysql_path=/usr/local/mysql #mysql安装路径
data_path=/data/mysql #mysql数据路径
groupadd mysql &> /dev/null
useradd -r -g mysql -s /bin/false mysql &> /dev/null
cd /usr/local
tar zxf $tar_path/mysql-*
ln -s /usr/local/mysql-* mysql
cd $mysql_path
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
cp support-files/mysql.server /etc/init.d/mysql.server
# 添加环境变量并生效
echo "export PATH=$PATH:$mysql_path/bin" >> /etc/profile && source /etc/profile
echo "export PATH=$PATH:$mysql_path/bin" >> /etc/bashrc && source /etc/bashrc
# 修改配置文件并建立设计到的文件目录,修改权限
touch /etc/my.cnf
echo "[mysqld]
#basedir=/opt/mysql/mysql
datadir=$data_path/data
log_error=$data_path/log/error.log
" > /etc/my.cnf
mkdir -p $data_path
mkdir $data_path/data
mkdir $data_path/log
chown -R mysql.mysql /data
chmod -R 750 /data
# 初始化,获取随机密码,并开启服务
cd $mysql_path && bin/mysqld --initialize --user=mysql
/etc/init.d/mysql.server start
password=`cat /data/mysql/log/error.log | grep root@localhost | tail -n 1 | awk '{print $11}'`
# 登录mysql并修改密码
mysql -uroot -p$password
set password for root@localhost = password('123456');
执行.sql脚本内容
mysql -uroot -p'123456'
source .sql文件的路径
source /root/test.sql