shell 自动安装 mysql 5.7.26
#/bin/bash
#下载对应官方版本
mysql_version=MySQL-5.7
file_name=mysql-5.7.26-linux-glibc2.12-x86_64
#安装路径
dir=/data1
mysql_dir=/data1/mysql
mysql_port=3306
#mysql 登陆密码
new_password=mysql@12345678
###########配置########################
#源码路径$dir/src
mkdir -p $dir/src
cd $dir/src
# 检测文件源码是否存在 /data1/src中
if [ -e $dir/src/$file_name.tar.gz ]
then
echo "源码文件 $file_name.tar.gz已经存在 $dir/src 中"
else
cd $dir/src
echo "$file_name.tar.gz不存在,从官网下载源码文件!请稍后..."
sleep 3
echo -e "\n"
wget https://dev.mysql.com/get/Downloads/$mysql_version/$file_name.tar.gz
sleep 1
fi
echo "解压$source_file安装文件"
cd $dir/src
echo "解压中,等待20s"
tar -zxvf $file_name.tar.gz >/dev/null 2>&1
mv $file_name mysql
mv mysql $dir
#############################################
echo "卸载mariadb rpm 包"
A=`rpm -qa mariadb*`
rpm -e --nodeps $A
sleep 1
####################
sleep 1
echo "创建mysql用户"
groupadd mysql
useradd -r -g mysql mysql
echo "新建mysql安装目录"
sleep 1
mkdir -p $dir/mysql/data
mkdir -p $dir/mysql/tmp
mkdir -p $dir/mysql/logs
mkdir -p $dir/mysql/etc
echo "创建mysql配置文件"
cat >> $dir/mysql/etc/my.cnf << EOF
[mysqld]
port = $mysql_port
#源码安装 环境变量 默认读取 这个路径,不然 就要指定sock ,否正登陆不了
socket = /tmp/mysql.sock
wait_timeout = 31536000
interactive_timeout = 31536000
basedir = $dir/mysql
datadir = $dir/mysql/data
pid-file = $dir/mysql/tmp/mysql.pid
event_scheduler = 1
log_error = $dir/mysql/logs/mysql-error.log
log_bin = $dir/mysql/logs/mysql-bin.log
#开启bin-log,并指定文件目录和文件名前缀
log_bin_trust_function_creators = 1
#数据库唯一ID,一组主从中此标识号不能重复。其中1 代表主数据库(源) 2代表辅数据库(目的)
server-id = 1
#需要同步的数据库名字,可以是多个,之间用分号分割
#binlog_do_db = zabbix
#binlog_do_db = mysql
#需要同步的数据库名字,可以是多个,之间用分号分割
#binlog-ignore-db = mysql
#binlog-ignore-db = sys
#binlog-ignore-db = performance_schema
max_binlog_size = 500M
#每个bin-log最大大小,当此大小等于500M时会自动生成一个新的日志文件。一条记录不会写在2个日志文件中,所以有时日志文件会超过此大小。
binlog_cache_size = 1M
#日志缓存大小
expire_logs_days = 2
#设置bin-log日志文件保存的天数,此参数mysql5.0以下版本不支持。
binlog_format = mixed
#bin-log日志文件格式,设置为MIXED可以防止主键重复
log_timestamps = system
slow_query_log = 1
long_query_time = 3
slow_query_log_file = $dir/mysql/logs/mysql-slow.log
#这个参数设置为ON,可以捕获到所有未使用索引的SQL语句
#log_queries_not_using_indexes = 1
user = mysql
bind-address = 0.0.0.0
skip-name-resolve
back_log = 600
max_connections = 10240
max_connect_errors = 10000
open_files_limit = 10240
table_open_cache = 128
max_allowed_packet = 1024M
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
thread_cache_size = 64 ##线程缓存
query_cache_size = 32M ##查询缓存大小
query_cache_limit = 2M
key_buffer_size = 4M
ft_min_word_len = 4
transaction_isolation = REPEATABLE-READ
performance_schema = 0
explicit_defaults_for_timestamp
skip-external-locking
default-storage-engine = InnoDB
#独立的表空间
innodb_file_per_table = 1
innodb_open_files = 10240
innodb_buffer_pool_size = 4096M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
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
innodb_lru_scan_depth = 128
innodb_io_capacity = 512
innodb_io_capacity_max = 1024
innodb_max_dirty_pages_pct = 70
innodb_lock_wait_timeout = 120
innodb_page_cleaners = 4
innodb_flush_method = O_DIRECT
innodb_buffer_pool_instances = 4
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
[mysql]
EOF
sleep 2
echo "授权用户权限配置"
sleep 1
chown -R mysql:mysql $dir/mysql
chown mysql:mysql $dir/mysql/etc/my.cnf
echo "初始化 mysql 数据库"
$dir/mysql/bin/mysqld --defaults-file=$dir/mysql/etc/my.cnf --initialize --user=mysql --basedir=$dir/mysql/ --datadir=$dir/mysql/data/ --pid-file=$dir/mysql/tmp/mysql.pid --socket=$dir/mysql/tmp/mysql.sock
sleep 2
echo "初始化 数据库完成"
sleep 1
echo "创建开机启动文件"
cat >> /etc/init.d/mysql << EOF
#!/bin/bash
# chkconfig: 345 64 60
# description: A very fast and reliable SQL database engine.
#引入系统函数库
. /etc/init.d/functions
#基础路径定义
basedir=$mysql_dir
bindir=\$basedir/bin
lockdir=\$basedir/tmp
lock_file_path=\$lockdir/mysql.sock.lock
mysqld_pid=\$basedir/tmp/mysql.pid
#成功提示函数
log_success_msg(){
#action为特殊的提示函数,\$@为所有参数。
action "SUCCESS! \$@" /bin/true
}
#失败提示函数
log_failure_msg(){
action "ERROR! \$@" /bin/false
}
#mysql启动函数
start(){
echo \$"Starting MySQL"
#测试mysqld_safe是否可执行
if test -x \$bindir/mysqld_safe
then
#后台执行启动mysql命令 #--socket 源码安装 默认 mysql 登陆命令,是寻找 /tmp/mysql.sock文件登陆,所以需要定义
\$bindir/mysqld_safe --defaults-file=\$basedir/etc/my.cnf --socket=/tmp/mysql.sock &>/dev/null &
#获取返回值
retval=\$?
#判断返回值是否为0
if [ \$retval -eq 0 ]
then
#调用成功提示函数。
log_success_msg "mysql Startup"
if test -w "\$lockdir" #判断锁目录是否可写。
then
touch "\$lock_file_path" #创建锁文件。
fi
return \$retval #给返回值是专业的表现。
else
log_failure_msg "MySQL Startup" #调用失败函数提示。
return \$retval
fi
else
log_failure_msg "Couldn't find MySQL server (\$bindir/mysqld_safe)"
fi
}
#停止MySQL函数。
stop(){
#判断mysql pid file大小是否为0。
if test -s "\$mysqld_pid"
then
#读取pidfile
mysqld_pid=\`cat "\$mysqld_pid"\`
#判断mysql pid对应的进程是否存在。
if (kill -0 \$mysqld_pid 2>/dev/null)
then
echo \$"Shutting down MySQL"
kill \$mysqld_pid #停止MySQL命令。
retval=\$?
if [ \$retval -eq 0 ]
then
log_success_msg "MySQL Stop" #调用停止成功函数。
if test -f "\$lock_file_path"
then
rm -f "\$lock_file_path" #删除锁文件。
fi
return \$retval
else
log_failure_msg "MySQL Stop."
return \$retval
fi
else
log_failure_msg "MySQL server process mysqld_pid is not running!"
rm "\$mysqld_pid"
fi
else
log_failure_msg "MySQL server PID file is null or not exist!"
fi
}
#接收传参判断并执行相应函数。
case "\$1" in
start)
start
retval=\$?
;;
stop)
stop
retval=\$?
;;
restart)
stop
sleep 8 #这里很重要,要休息一下。
start
retval=\$?
;;
*)
echo \$"Usage:\$0 {start|stop|restart}"
exit 2
esac
exit \$retval
EOF
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
sleep 1
echo "创建启动文件,添加开机启动完成"
echo "启动mysql"
#centos 7 需要重新加载 daemon-reload 才能让启动文件生效。
# 直接 /etc/init.d/mysql 启动会失败
start_mysql(){
cd /etc/init.d/
./mysql start >/dev/null 2>&1
sleep 3
}
start_mysql
if [ -n "`ps -ef |grep -v grep |grep mysql |awk '{print $2}'`" ]
then
ps -ef |grep -v grep |grep mysql |awk '{print $2}' |xargs kill -9
else
start_mysql
ps -ef |grep -v grep |grep mysql |awk '{print $2}' |xargs kill -9
fi
start_mysql
echo "修改mysql登陆密码"
password=`cat $dir/mysql/logs/mysql-error.log |grep "A temporary password is generated for root@localhost" |awk '{print $NF}'`
echo $password >> $dir/mysql/tmp/mysql-password.txt
# 修改 登陆密码
sleep 2
$dir/mysql/bin/mysql -uroot -p${password} --connect-expired-password -e "alter user 'root'@'localhost' identified by '$new_password';" >/dev/null 2>&1
sleep 2
$dir/mysql/bin/mysql -uroot -p$new_password --connect-expired-password -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$new_password' WITH GRANT OPTION;" >/dev/null 2>&1
sleep 3
echo "配置mysql 密码,连接权限 完成"
echo "配置环境变量"
echo "export PATH=$dir/mysql/bin:\$PATH" >> /etc/profile
sleep 2
source /etc/profile
sleep 1
echo "配置环境变量完成"
echo "安装mysql 完成"
本文是原创文章,转载请注明来自 知识殿堂
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果