linux下mysql8 shell脚本一键安装

1:下载mysql8文件

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.18-el7-x86_64.tar.gz

2:新建一个my.cnf文件

vi my.cnf

[mysqld]

#skip-grant-tables

#collation_server=utf8_general_ci

#character_set_server=utf8

collation_server=utf8mb4_zh_0900_as_cs

character_set_server=utf8mb4

event_scheduler=ON

port=3390

mysqlx_port=33900

max_connections=1000

innodb_buffer_pool_size=3072M

table_open_cache=4000

key_buffer_size=384M

max_allowed_packet=4M

sort_buffer_size=1M

read_buffer_size=1M

read_rnd_buffer_size=8M

myisam_sort_buffer_size=64M

thread_cache_size=64

default_authentication_plugin=mysql_native_password

datadir=/app/software/mysql8/data

socket=/app/software/mysql8/mysql.sock

#log-bin=mysql-bin 

lower_case_table_names=1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

symbolic-links=0

[mysqld_safe]

log-error=/app/software/mysql8/mysql.log

pid-file=mysql.pid

3: 把下载好的mysql安装文件和my.cnf 放在同一个目录下

4:新建一键安装mysql脚本install_mysql8.sh

vi install_mysql8.sh

#!/bin/bash

#shell install Mysql8

#LiuQianMing  2019-11-20 16:55:10

#tar gz

tar -zxvf mysql-8.0.18-el7-x86_64.tar.gz

mv mysql-8.0.18-el7-x86_64 mysql8

cp my.cnf mysql8

cd mysql8

#当前登录用户

currentUser=$USER

#mysql 访问端口

mysqlPort=3390

#mysql 插件 端口

mysqlxPort=33900

#mysql root 密码 根据实际需要设置

mysqlRootPasswd='xxxxxxx'

#mysql新增普通用户名 根据实际需要设置

mysqlPublicUser=xxxx

#mysql新增普通用户密码 根据实际需要设置

mysqlPublicUserPasswd='xxxxx'

mysqlDir=$(cd $(dirname $0); pwd)

mkdir data

echo $(date +'%Y-%m-%d %H:%M:%S'):'开始初始化数据库启动文件my.cnf'


#sed -i '/mysqld/a\datadir=$mysqlDir/data' my.cnf;

sed -i '/^datadir=/c'\datadir=$mysqlDir\/data'' my.cnf

#sed -i '/mysqld/a\basedir=$mysqlDir' my.cnf;

sed -i '/^basedir=/c'\basedir=$mysqlDir'' my.cnf

#sed -i '/mysqld/a\port=3390' my.cnf;

sed -i '/^port=/c'\port=$mysqlPort'' my.cnf

#sed -i '/mysqld/a\port=3390' my.cnf;

sed -i '/^mysqlx_port=/c'\mysqlx_port=$mysqlxPort'' my.cnf

#sed -i '/mysqld/a\character_set_server=utf8mb4' my.cnf;

sed -i '/^character_set_server=/c'\character_set_server=utf8mb4'' my.cnf

#sed -i '/mysqld/a\collation_server=utf8mb4_zh_0900_as_cs' my.cnf;

sed -i '/^collation_server=/c'\collation_server=utf8mb4_zh_0900_as_cs'' my.cnf

#sed -i '/mysqld/a\event_scheduler=ON' my.cnf;

sed -i '/^event_scheduler=/c'\event_scheduler=ON'' my.cnf

#sed -i '/mysqld/a\socket=$mysqlDir/mysql.sock' my.cnf;

sed -i '/^socket=/c'\socket=$mysqlDir\/mysql.sock'' my.cnf

#sed -i '/mysqld/a\lower_case_table_names=1' my.cnf;

sed -i '/^lower_case_table_names=/c'\lower_case_table_names=1'' my.cnf

#sed -i '/mysqld/a\default_authentication_plugin=mysql_native_password' my.cnf;

sed -i '/^default_authentication_plugin=/c'\default_authentication_plugin=mysql_native_password'' my.cnf

#sed -i '/mysqld/a\skip-grant-tables' my.cnf;

#sed -i '/^skip-grant-tables=/c'\#skip-grant-tables'' my.cnf

sed -i '/^log-error=/c'\log-error=$mysqlDir\/mysql.log'' my.cnf

sed -i '/^pid-file=/c'\pid-file=$mysqlDir\/mysql.pid'' my.cnf

echo $(date +'%Y-%m-%d %H:%M:%S'):'开始初始化数据库启动文件mysql.server'

cp support-files/mysql.server $mysqlDir

#sed -i '46c\basedir=$mysqlDir' mysql.server;

sed -i '/^basedir=/c'\basedir=$mysqlDir'' mysql.server

#sed -i '47c\datadir=$mysqlDir/data' mysql.server;

sed -i '/^datadir=/c'\datadir=$mysqlDir\/data'' mysql.server

echo $(date +'%Y-%m-%d %H:%M:%S'):'开始初始化数据库'

$mysqlDir/bin/mysqld  --defaults-file=$mysqlDir/my.cnf  --initialize --user=$USER  --basedir=$mysqlDir --datadir=$mysqlDir/data --log-error=$mysqlDir/mysql.log

echo $(date +'%Y-%m-%d %H:%M:%S'):'数据库初始化完成'

echo $(date +'%Y-%m-%d %H:%M:%S'):'开始启动数据库'

$mysqlDir/mysql.server start

#修改mysql登录密码

tempPasswd=`grep  'temporary password'  $mysqlDir/mysql.log`

mysqlPasswd=`echo ${tempPasswd##*localhost:}` 

echo $(date +'%Y-%m-%d %H:%M:%S'):"临时密码:$mysqlPasswd"

echo $(date +'%Y-%m-%d %H:%M:%S'):'修改root密码'

mysql -uroot -p"${mysqlPasswd}" -S  $mysqlDir/mysql.sock -e  "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$mysqlRootPasswd'"  --connect-expired-password

echo $(date +'%Y-%m-%d %H:%M:%S'):'修改root密码成功'

echo $(date +'%Y-%m-%d %H:%M:%S'):'创建blekey用户'

mysql -uroot -p"$mysqlRootPasswd" -S  $mysqlDir/mysql.sock -e  "CREATE USER '$mysqlPublicUser'@'%' IDENTIFIED BY '$mysqlPublicUserPasswd'"  --connect-expired-password

echo $(date +'%Y-%m-%d %H:%M:%S'):'创建xxx用户成功'

echo $(date +'%Y-%m-%d %H:%M:%S'):'赋权x x x用户'

mysql -uroot -p"$mysqlRootPasswd" -S  $mysqlDir/mysql.sock -e  "GRANT ALL ON *.* TO '$mysqlPublicUser'@'%'"  --connect-expired-password

echo $(date +'%Y-%m-%d %H:%M:%S'):'赋权xxx用户成功'

echo $(date +'%Y-%m-%d %H:%M:%S'):'刷新权限'

mysql -uroot -p"$mysqlRootPasswd" -S  $mysqlDir/mysql.sock -e  "FLUSH PRIVILEGES"  --connect-expired-password

echo $(date +'%Y-%m-%d %H:%M:%S'):'刷新权限成功'

#如果当前用户是root配置etc下,如果是普通用户则配置到对应用户下的环境变量

if [ "$currentUser" = "root" ];

  then

    sed  -i '$a  \export PATH='$mysqlDir'\/bin:$PATH'  /etc/profile

    source /etc/profile

  else

    sed  -i '$a  \export PATH='$mysqlDir'\/bin:$PATH'  /home/"$currentUser"/.bash_profile

    source /home/"$currentUser"/.bash_profile

fi

#sed -i '/^skip-grant-tables=/c'\#skip-grant-tables'' my.cnf

#$mysqlDir/mysql.server restart

你可能感兴趣的:(linux下mysql8 shell脚本一键安装)