参考
MySQL-5.7
实践
vim mysql.sh
#!/bin/bash
v_fileName=$1
#获取当前脚本路径
v_curPath=$(cd `dirname $0`; pwd)
v_log=${v_curPath}"/"$0".log"
#声明消息提醒前缀方法
f_msg(){
echo $(date "+%Y-%m-%d %H:%M:%S")$LINENO" "$1"==>"
}
#判断日志文件是否存在若不存在则创建
if [ ! -f ${v_log} ]
then
touch ${v_log}
echo $(f_msg INFO)$LINENO" 创建日志文件" >> ${v_log}
else
echo "=======================================">>${v_log}
fi
#校验工具是否存在方法
f_checkRpm(){
v_name1=$1
v_check=$(rpm -qa ${v_name1})
if [ ${#v_check} -eq 0 ]
then
echo $(f_msg ERROR)$LINENO " "${v_name1}"工具不存在,请安装"${v_name1}"工具">>${v_log}
return 0
else
echo $(f_msg INFO)$LINENO " "${v_name1}"工具的版本号为"${v_check}>>${v_log}
return 1
fi
}
v_yum=$(rpm -qa yum)
if [ ${#v_yum} -eq 0 ]
then
echo $(f_msg ERROR)$LINENO" 未检测到yum,请安装yum"
echo $(f_msg ERROR)$LINENO" 未检测到yum,请安装yum" >> ${v_log}
exit 0
else
echo $(f_msg INFO)$LINENO" yum的版本为"${v_yum} >> ${v_log}
fi
v_tcl=$(rpm -qa tcl)
if [ ${#v_tcl} -eq 0 ]
then
echo $(f_msg INFO)$LINENO" 未检测到tcl,请安装tcl"
echo "执行语句 yum -y install tcl "
echo $(f_msg INFO)$LINENO" 未检测到tcl,请安装tcl" >> ${v_log}
exit 0
else
echo $(f_msg INFO)$LINENO" tcl的版本为"${v_tcl} >> ${v_log}
fi
v_expect=$(rpm -qa expect)
if [ ${#v_expect} -eq 0 ]
then
echo $(f_msg INFO)$LINENO" 未检测到expect,请安装expect"
echo "执行语句 yum -y install expect "
echo $(f_msg INFO)$LINENO" 未检测到expect,请安装expect" >> ${v_log}
exit 0
else
echo $(f_msg INFO)$LINENO" expect的版本为"${v_expect} >> ${v_log}
fi
#判断文件是否存在,若不存在则下载
if [ ${#v_fileName} -eq 0 ]
then
f_checkRpm wget
v_rqmReturn=$?
#检测当前目录下是否有mysql文件
if [ ${v_rqmReturn} -eq 0 ]
then
echo $(f_msg INFO)$LINENO" 未检测到wget,请安装wget" >> ${v_log}
exit 0
fi
v_mysqlC=$(ls -ld mysql* |grep ^-|wc -l)
if [ ${v_mysqlC} -lt 1 ]
then
#若不存在则下载
wget http://dev.MySQL.com/get/Downloads/MySQL-5.7/mysql-5.7.11-Linux-glibc2.5-x86_64.tar.gz
fi
else
if [ ! -f ${v_fileName} ]
then
wget http://dev.MySQL.com/get/Downloads/MySQL-5.7/${v_fileName}
fi
fi
#检验tar是否存在
v_tar=$(rpm -qa tar)
if [ ${#v_tar} -le 0 ]
then
echo $(f_msg ERROR)$LINENO" tar工具不存在,请安装tar工具">>${v_log}
exit 0
else
echo $(f_msg INFO)$LINENO" tar工具的版本号为"${v_tar}>>${v_log}
fi
#获取文件名
for fileName in `ls -d mysql*`
do
if [ -f fileName ]
then
v_fileName=${fileName}
fi
done
echo $(f_msg INFO)$LINENO" 获取mysql压缩包文件名"${v_fileName}>>${v_log}
#解压文件
mkdir mysql
tar -xvf ${v_fileName} -C mysql
cd mysql
v_mysqlChildC=$(ls -l|grep ^d|wc -l)
v_tempFileName=""
for fileName in `ls`
do
v_tempFileName=${fileName}
done
cd ${v_tempFileName}
mv * ../
cd ..
rm -rf ${v_tempFileName}
#创建data目录
mkdir -p /usr/local/mysql/data
#创建mysql用户组及其用户
if [ ! $(grep mysql /etc/group) ]
then
groupadd mysql
echo $(f_msg INFO)$LINENO" 创建mysql组成功">>${v_log}
else
echo $(f_msg INFO)$LINENO" mysql组已存在无需创建">>${v_log}
fi
#判断密码是否输入,若不输入则默认值为root
v_pwd=${2}
if [ ${#v_pwd} -eq 0 ]
then
v_pwd="mmsql"
fi
#创建用户
if [ ! $(grep mysql /etc/passwd) ]
then
useradd -r -s /sbin/nologin -g mysql mysql -p v_pwd
echo $(f_msg INFO)$LINENO" 创建mysql用户成功">>${v_log}
else
echo $(f_msg INFO)$LINENO" mysql组已存在无需创建">>${v_log}
fi
#初始化mysql数据
cd /usr/local/mysql/
./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
echo $(f_msg INFO)$LINENO" 初始化mysql数据">>${v_log}
#判断my.cnf 是否存在若存在则删除
if [ $(ls -l /etc/my.cnf | wc -l) -ne 0 ]
then
rm -rf /etc/my.cnf
fi
if [ $(ls -l /etc/init.d/mysqld | wc -l) -ne 0 ]
then
rm -rf /etc/init.d/mysqld
fi
#拷贝文件
cp -a ./support-files/my-default.cnf /etc/my.cnf
cp -a ./support-files/mysql.server /etc/init.d/mysqld
echo $(f_msg INFO)$LINENO" 拷贝mysql配置文件">>${v_log}
#修改my.cnf内容
sed -i "s/# basedir = ...../basedir = \/usr\/local\/mysql/g" /etc/my.cnf
sed -i "s/# datadir = ...../datadir = \/usr\/local\/mysql\/data/g" /etc/my.cnf
sed -i "s/# port = ...../port = 3306/g" /etc/my.cnf
sed -i "s/# socket = ...../socket = \/tmp\/mysql.sock/g" /etc/my.cnf
#echo "skip-grant-tables " >>/etc/my.cnf
#判断character-set-server 是否存在,若存在则删除
if [ $(grep character-set-server /etc/my.cnf) ]
then
sed -i "/character-set-server/d" /etc/my.cnf
fi
echo "character-set-server = utf8" >> /etc/my.cnf
if [ $(grep client /etc/my.cnf) ]
then
sed -i "/client/d" /etc/my.cnf
fi
#echo "[client]" >> /etc/my.cnf
#echo "host = localhost" >> /etc/my.cnf
#echo "user = root" >> /etc/my.cnf
#echo "password ="123456 >> /etc/my.cnf
#echo $(f_msg INFO)$LINENO" 修改my.cnf文件">>${v_log}
#将mysql 添加到环境变量中
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
#重新加载profile文件
/usr/bin/expect<<EOF
set timeout -1
spawn su - root
expect "*"
send "source /etc/profile\n"
expect "*"
send "exit\n"
expect eof
EOF
#source /etc/profile
#echo $(f_msg INFO)$LINENO" 配置MySQL的环境变量,使mysql命令生效">>${v_log}
#创建In
ln -s /usr/local/mysql/ /usr/bin/
#启动服务
service mysqld start
#获取mysql 初始密码
v_mysqlinitpwd=$(cat /root/.mysql_secret | awk 'END {print}')
#修改MySQL初始密码
<<!
/usr/local/mysql/bin/mysql -u root -p${v_mysqlinitpwd} -e "alter user root@localhost identified by '123456';flush privileges;"
echo $(f_msg INFO)$LINENO" 修改root用户初始密码">>${v_log}
#查询mysql用户是否有外网登录权限
v_msyqlC=$(/usr/local/mysql/bin/mysql -N -s -u root -p${v_mysqlinitpwd} -D mysql -e "select count(1) from user where host ='%' and user='mysql';")
if [ ${v_msyqlC} -le 0 ]
then
#创建用户mysql
/usr/local/mysql/bin/mysql -N -s -u root -p${v_mysqlinitpwd} -D mysql -e "CREATE USER 'mysql'@'host' IDENTIFIED BY '123456';flush privileges;"
#给用户mysql 授权
/usr/local/mysql/bin/mysql -N -s -u root -p${v_mysqlinitpwd} -D mysql -e "grant all privileges on *.* to mysql@'%' identified by '123456';flush privileges;"
fi
echo $(f_msg INFO)$LINENO" 添加mysql用户,并给mysql用户授权">>${v_log}
!
/usr/bin/expect<<EOF
set timeout -1
spawn su - root
expect "*" { send "/usr/local/mysql/bin/mysql -u root -p\n" }
expect "Enter password:" { send "${v_mysqlinitpwd}\n" }
expect "mysql>" { send "alter user root@localhost identified by '123456';\n" }
expect "mysql>" { send "flush privileges;\n" }
expect "mysql>" { send "use mysql;\n"}
expect "mysql>" { send "CREATE USER 'mysql'@'host' IDENTIFIED BY '123456';\n" }
expect "mysql>" { send "flush privileges;\n" }
expect "mysql>" { send "grant all privileges on *.* to mysql@'%' identified by '123456';\n" }
expect "mysql>" { send "flush privileges;\n" }
expect "mysql>" { send "exit\n" }
expect "*"
send "exit\n"
expect eof
EOF
echo $(f_msg INFO)$LINENO" 初始化数据库信息">>${v_log}
#设置开机启动 mysql
systemctl enable mysqld
systemctl daemon-reload
echo $(f_msg INFO)$LINENO" 设置mysql开机启动">>${v_log}
echo "需要手动执行 source /etc/profile"
echo "如外网无法调用请检查防火墙的端口是否打开"
echo "执行完毕"
注意
- 133 行创建用户时 不要用这里的密码请自行设置防止被攻击
- 数据库默认密码为123456请提前更换后执行脚本或mysql启动后更改
- 脚本启动是要设置 chmod -R 777 xx.sh 要不然不能执行sh文件
- 脚本要放置在 usr/local/文件夹下 要不然会报
No such file or directory
的错- 我尝试修改过,没改成功。
- mysql文件可以使用离线的
https://renwole.com/archives/2215
#!/bin/env bash
# =================================================
# Author: renwole.com #
# Description: MySQL automatic installation tool #
# =================================================
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
Install_MySQL() {
# CentOS 8 mysql: error while loading shared libraries: libncurses.so.5
yum install ncurses ncurses-compat-libs -y
# Install dependencies
setenforce 0
yum -y install epel-release
yum -y install wget tar jemalloc jemalloc-devel gcc gcc-c++
# MySQL configuration
mysql_version="8.0.21"
mysql_password="111"
mkdir -p /apps/server/mysql/data
mysql_install_dir="/apps/server/mysql"
mysql_data_dir="/apps/server/mysql/data"
download_mysql="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads"
# Download MySQL
cd /mnt
useradd -M -s /sbin/nologin mysql >/dev/null 2>&1
wget -c ${download_mysql}/MySQL-8.0/mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz
echo "========== Start installing MySQL ============"
tar xJf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz
\mv mysql-${mysql_version}-linux-glibc2.12-x86_64/* ${mysql_install_dir}
if [[ -d "${mysql_install_dir}/support-files" ]]; then
rm -rf mysql-${mysql_version}-linux-glibc2.12-x86_64
else
rm -rf ${mysql_install_dir}
echo "Error: MySQL install failed, Please contact the author"
kill -9 $$
fi
# Initialize the database
${mysql_install_dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${mysql_install_dir} --datadir=${mysql_data_dir}
chown -R root . ${mysql_install_dir}
chown -R mysql.mysql ${mysql_data_dir}
\cp -f ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
sed -i "s@^basedir=.*@basedir=${mysql_install_dir}@" /etc/init.d/mysqld
sed -i "s@^datadir=.*@datadir=${mysql_data_dir}@" /etc/init.d/mysqld
sed -i "s@/usr/local/mysql@${mysql_install_dir}@g" ${mysql_install_dir}/bin/mysqld_safe
sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' ${mysql_install_dir}/bin/mysqld_safe
echo 'PATH=$PATH:'${mysql_install_dir}'/bin' >/etc/profile.d/mariadb_renwole.com.sh
# my.conf configuration
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysql]
prompt="MySQL [\\d]> "
no-auto-rehash
[mysqld]
port = 3306
socket = /tmp/mysql.sock
default_authentication_plugin = mysql_native_password
basedir = ${mysql_install_dir}
datadir = ${mysql_data_dir}
pid-file = ${mysql_data_dir}/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
collation-server = utf8mb4_0900_ai_ci
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
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
binlog_expire_logs_seconds = 604800
log_error = ${mysql_data_dir}/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = ${mysql_data_dir}/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
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF
# Set up MySQL
chmod 600 /etc/my.cnf
chmod +x /etc/init.d/mysqld
systemctl enable mysqld
systemctl start mysqld
${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "create user root@'127.0.0.1' identified by \"${mysql_password}\";"
${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'127.0.0.1' with grant option;"
${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'localhost' with grant option;"
${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "alter user root@'localhost' identified by \"${mysql_password}\";"
${mysql_install_dir}/bin/mysql -uroot -p${mysql_password} -e "reset master;"
rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona,alisql}*.conf
echo "${mysql_install_dir}/lib" > /etc/ld.so.conf.d/mysql_renwole.com.conf
ldconfig
echo "========== MySQL installing Successfully ====="
echo
echo "=============================================="
echo " Thank you for using Renwole.com script"
echo "=============================================="
echo "MySQL:"
echo "account: root"
echo "password: ${mysql_password}"
echo "database: ${mysql_data_dir}"
echo "=============================================="
}
Install_MySQL
source /etc/profile