安装mysql5.7(局域网非root账号)

创建mysql安装目录路径
mkdir /home/webuser/software

上传mysql安装包 mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
到该目录下

创建安装脚本
vi mysql_install.sh

#!/bin/bash
set -o errexit # 报错则中止执行
export mysql_path=/home/webuser/software
export port=13306
export user=wxcep
export pwd=Weiyan@2021
export osuser=webuser
 
export base_soft_path=$(cd `dirname $0`; pwd)
echo "当前安装路径为:"$base_soft_path
 
#解压、重命名
echo "解压安装包..."
tar -zxvf $base_soft_path/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
mv $base_soft_path/mysql-5.7.38-linux-glibc2.12-x86_64/ $base_soft_path/mysql
mysql_path=$base_soft_path/mysql
 
#添加配置文件
cat >> $mysql_path/my.cnf<<EOF
[client]
port=$port                              #服务端口
socket=$mysql_path/mysql.sock           #指定套接字文件
default-character-set = utf8mb4
[mysqld]
port=$port                              #服务端口
basedir=$mysql_path                     #mysql安装路径
datadir=$mysql_path/data                #数据目录
pid-file=$mysql_path/mysql.pid          #指定pid文件
socket=$mysql_path/mysql.sock           #指定套接字文件
log_error=$mysql_path/error.log         #指定错误日志
server-id=100                           #Mysql主从唯一标识
default-storage-engine = INNODB
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true 
  
[mysql] 
default-character-set = utf8mb4
EOF
 
echo "安装启动mysql,请等待..."
cd $mysql_path/bin
./mysqld --defaults-file=$mysql_path/my.cnf --initialize --user=$osuser --basedir=$mysql_path --datadir=$mysql_path/data
./mysqld_safe --defaults-file=$mysql_path/my.cnf --user=$osuser &
#停止5秒,等待启动完成
sleep 5
cd $mysql_path
temp_pwd=$(grep 'A temporary password' error.log |awk  '{print $NF}')
echo "获取临时密码:"$temp_pwd
 
#更新密码、新增用户super、开启远程访问
cd $mysql_path/bin
echo "登入mysql"
./mysql --connect-expired-password -P$port -uroot -p$temp_pwd -S $mysql_path/mysql.sock -e"
set password for 'root'@'localhost' = password('$pwd');
flush privileges;
use mysql;
GRANT ALL PRIVILEGES ON *.* TO '$user'@'%' IDENTIFIED BY '$pwd';
flush privileges;
exit"
echo "完成更新密码,开启远程访问"
echo "安装完成!"

其中
export mysql_path=安装路径
export port=数据库端口
export user=数据库用户名
export pwd=数据库密码
export osuser=操作系统用户名
mysql-5.7.38-linux-glibc2.12-x86_64 是对应mysql安装包;
以上信息根据实际情况,修改

创建启动脚本
vi mysql_start.sh

#!/bin/bash
set -o errexit # 报错则中止执行
export mysql_path=/home/webuser/software
export base_soft_path=$(cd `dirname $0`; pwd)
echo "当前安装路径为:"$base_soft_path
mysql_path=$base_soft_path/mysql
 
# 启动
cd $mysql_path/bin
./mysqld_safe --defaults-file=$mysql_path/my.cnf --user=admin &
sleep 2
echo "mysql启动成功."

创建启动脚本
vi mysql_start.sh

#!/bin/bash
set -o errexit # 报错则中止执行
export mysql_path=/home/webuser/software
export base_soft_path=$(cd `dirname $0`; pwd)
echo "当前安装路径为:"$base_soft_path
mysql_path=$base_soft_path/mysql
 
#验证用户名密码
read -p "请输入mysql用户名:" user
echo -e "请输入mysql密码: \c"
while : ;do
	char=`  stty cbreak -echo
			dd if=/dev/tty bs=1 count=1 2>/dev/null
			stty -cbreak echo
	`
	if [ "$char" =  "" ];then
		echo  #换行
		break
	fi
	pwd="$pwd$char"
	echo -n "*" #使用*代替密码
done
# 关闭
cd $mysql_path/bin
./mysqladmin -u$user -p$pwd -S $mysql_path/mysql.sock shutdown
sleep 2
echo "mysql关闭成功."

赋予三个文件执行权限

chmod+x  mysql_install.sh
chmod+x  mysql_start.sh
chmod+x  mysql_stop.sh

你可能感兴趣的:(#,关系型数据库,mysql,linux,bash)