说明:此脚本,适合glibc2.12所有版本的数据库安装。
安装包下载地址:MySQL :: Download MySQL Community Server
#!/bin/bash
echo "-----------------------------开始MYSQL安装--------------------------------------"
start_time=$(date +%s)
echo -e "\e[31m***************一键安装mysql任何版本数据库******************\e[0m"
echo -e "输入安装版本,如\e[31m8.0.27\e[0m"
read version
#find / -name mysql | xargs rm -rf
port=$(netstat -antup|grep mysql|wc -l)
if [ $port != 0 ]
then echo "mysql进程存在,请先杀掉进程"
ps -ef |grep mysqld
exit 1
fi
echo "-----------------创建所需目录及用户并上传安装包----------------------------"
# 获取当前所在目录位置
current_dir=$(pwd)
echo "当前所在目录位置: $current_dir"
# 目标路径
target_dir="/opt"
# 检查目标路径是否存在,如果不存在则创建
if [ ! -d "$target_dir" ]; then
mkdir -p "$target_dir"
echo "已创建目录: $target_dir"
fi
# 移动当前目录下的所有文件到目标路径
mv $current_dir/mysql* $target_dir
echo "已将当前目录下所有文件移动至 $target_dir"
mkdir -p /data/mysql
groupadd mysql
useradd -r -g mysql mysql
cd /opt/
tar -xvf mysql-$version-linux-glibc2.12-x86_64.tar.xz
mv mysql-$version-linux-glibc2.12-x86_64/ /usr/local/
cd /usr/local/
mv mysql-$version-linux-glibc2.12-x86_64/ mysql
chown -R mysql.mysql /usr/local/mysql/
echo "-----------------------------卸载原有的mysql组件--------------------------"
yum list installed | grep mariadb
yum remove mariadb* -y >/dev/null 2>&1
chown mysql:mysql -R /data/mysql
touch /etc/my.cnf
chmod 644 /etc/my.cnf
MYSQL_ROOT_PASSWORD=123456
cat <<EOF >/etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
server_id=1
port=3306
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
tmp_table_size = 32M
max_heap_table_size = 32M
sync_binlog = 1
innodb_flush_log_at_trx_commit = 1
innodb_flush_method=O_DIRECT
innodb_use_fdatasync = ON
character-set-server=utf8mb4
innodb_rollback_on_timeout = ON
innodb_file_per_table=on
innodb_stats_on_metadata=on
innodb_autoinc_lock_mode=2
log_queries_not_using_indexes =1
innodb_print_all_deadlocks=1
auto_increment_increment=1
auto_increment_offset=1
log_throttle_queries_not_using_indexes = 60
interactive_timeout = 600
wait_timeout = 600
lock_wait_timeout = 3600
innodb_buffer_pool_size=2G
max_binlog_size = 1073741824
expire_logs_days=7
lower_case_table_names=1
max_connections=10000
binlog_format=row
log-bin=mysql-bin
gtid-mode=on
log-slave-updates=1
enforce-gtid-consistency=on
master-verify-checksum = 1
slave_skip_errors=1062
slave-skip-errors=all
default_authentication_plugin=mysql_native_password
[mysql]
socket=/tmp/mysql.sock
prompt="\u@\h:\d>"
[client]
EOF
echo "-----------------------------------初始化数据库-----------------------------------"
cd /usr/local/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
path=$(grep 'basedir' /etc/profile|wc -l)
if [ $path != 0 ]
then
echo -e "\e[31m MYSQL_HOME路径存在\e[0m"
else
echo "export basedir=/usr/local/mysql/bin" >> /etc/profile
echo "export PATH=\$PATH:\$basedir" >> /etc/profile
source /etc/profile
fi
echo "---------------------------------启动MYSQL服务---------------------------------------"
service mysql start
echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib'>>/etc/profile
sleep 3
source /etc/profile
cat /data/mysql/mysql.err|grep password
chkconfig --add mysql
chkconfig mysql on
chkconfig --list mysql
echo "-----------------------------恭喜!MYSQL安装成功--------------------------------------"
end_time=$(date +%s)
execution_time=$((end_time - start_time))
echo "脚本执行时间:${execution_time} 秒"
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.err`
mysqladmin -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD}
mysql -uroot -p123456 -e "update mysql.user set host ='%' where user ='root';flush privileges;"
mysql -uroot -p123456 -e "grant BACKUP_ADMIN on *.* to 'root'@'%';flush privileges;"
mysql -uroot -p123456
欢迎关注公众号《小周的数据库进阶之路》,更多精彩知识和干货尽在其中。
标签:万能,MySQL,一键,echo,etc,innodb,usr,mysql,local From: https://blog.csdn.net/qq_36936192/article/details/142974058