MySQL数据库一键部署脚本
MySQL数据库的一键安装脚本
#/bin/bash
###############变量名
#安装包
install_page=mysql-5.7.36-el7-x86_64.tar.gz
#解压目录
unzip_path=/data/mysql
#安装路径
install_path=/data
#数据存储目录
data=/data/data
#日志存放目录
logs_file=/data/logs
#mysql密码
mysql_passwd=root@1234
#临时缓存区目录
tmp_path=/data/tmp
#配置文件(方便后续需求更改)
conf=my.cnf
###################################分隔符################################################
#创建用户和组
groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
#检查是否存在mariadb
rpm -qa | grep mariadb
#这个变量是mariadb的包的名称(各个版本的系统包不一样)
mariadb=`rpm -qa | grep mariadb`
#这个变量是查询包的结果,如果是0则是执行成功,如果是大于0的任何数则为没有包
a=$?
if [[ $a -eq 0 ]]; then
echo “有mariadb”
rpm -e $mariadb --nodeps
else
echo "没有mariadb"
fi
#检查目录是否存在,不存在则创建
for dirname in {$install_path,$data,$logs_file,$tmp_path,$unzip_path};do
if [ -d "dirname" ];then
echo "文件夹存在"
chown -R mysql:mysql $dirname
else
mkdir -p $dirname
chown -R mysql:mysql $dirname
fi
done
#解压安装包并重命名
#--strip-components 1 解压出来没有一级目录
tar zxf $install_page -C $unzip_path --strip-components 1
#移动配置文件
if [ -f "/etc/my.cnf" ];then
echo "/etc/my.cnf已存在"
chmod 644 /etc/my.cnf
else
cp -rp $conf /etc/my.cnf
chmod 644 /etc/my.cnf
fi
#无密码初始化
$unzip_path/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize-insecure
#启动数据库
$unzip_path/support-files/mysql.server start
#为数据库的各种命令做环境变量
echo export MYSQL_HOME=${unzip_path} >> /etc/profile.d/mysqld.sh
echo export PATH=$\MYSQL_HOME/\bin:$\MYSQL_HOME/\lib:$\PATH\ >> /etc/profile.d/mysqld.sh
chmod +x /etc/profile.d/mysqld.sh
source /etc/profile.d/mysqld.sh
#修改可以登录的主机
update_sql="update mysql.user set host = '%' where user = 'root';"
mysql -uroot -e"${update_sql}"
mysql -uroot -e"flush privileges;"
#修改密码
pass_sql="ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '${mysql_passwd}'"
mysql -uroot -e"${pass_sql}"
mysql -uroot -p"${mysql_passwd}" -e"flush privileges;"
#复制MySQL的启动文件
cp -rp $unzip_path/support-files/mysql.server /etc/init.d/mysql.server
MySQL配置文件(注:配置文件中的文件地址需要更改)
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /data/mysql
datadir = /data/data
pid-file = /data/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
#导入函数
log-bin-trust-function-creators=1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /data/logs/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/logs/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#不区分大小写
lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
sql_mode= 'IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'
[mysqldump]
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
源码与安装包下载:
链接:https://pan.baidu.com/s/1gm_98lfdXKZzQzMxJGZBsg?pwd=4jr3
提取码:4jr3