MySQL安装脚本
Posted 锦衣admin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL安装脚本相关的知识,希望对你有一定的参考价值。
操作系统 | MySQL版本 |
---|---|
CentOS7.6最小化安装 | mysql-5.7.31 |
IP | 10.1.1.10 |
---|---|
安装目录 | /usr/local/mysql |
数据目录 | /usr/local/mysql/data |
套接字 | /tmp/mysql.sock |
CentOS 7初始化
操作系统初始化脚本:
#!/bin/bash
# Desc:系统初始化
os_init () {
hostnamectl set-hostname $name
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i.bak '/^SELINUX=/c\\SELINUX=disabled' /etc/selinux/config
sed -i.bak '/^UUID=/c\\UUID="af42de51-8836-44d0-8b20-1ebfa7818bc7"' $nic_name
sed -i '4a\\IPADDR=10.1.1.10' $nic_name
sed -i '/^BOOTPROTO="/c\\BOOTPROTO="static"' $nic_name
sed -i '5a\\DNS2=114.114.114.114' $nic_name
sed -i '5a\\DNS1=8.8.8.8' $nic_name
sed -i '5a\\GATEWAY=10.1.1.2' $nic_name
sed -i '5a\\NETMASK=255.255.255.0' $nic_name
systemctl stop NetworkManager
systemctl disable NetworkManager
systemctl restart network
if !(rpm -q wget);then
yum install -y wget
fi
tar -zcPf /etc/yum.repos.d/repo.tar.gz /etc/yum.repos.d/C*.repo
rm -f /etc/yum.repos.d/*.repo
wget -O /etc/yum.repos.d/CentOS-tencent-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
yum clean all
yum makecache
}
main () {
nic_name=/etc/sysconfig/network-scripts/ifcfg-ens33
read -p "Host name is:" name
os_init
yum install bash-completion vim net-tools ntp -y
}
main
MySQL5.7安装
MySQL安装脚本:
#!/bin/bash
# Desc:MySQL安装脚本
# 根据版本安装
mysql_install () {
[ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak
if ! (id mysql &>/dev/null);then
useradd -r -s /sbin/nologin mysql
fi
if [ -n "$mysql_glibc" ];then
mkdir /usr/local/mysql && tar xf $mysql_glibc.tar.gz -C /usr/local/mysql --strip-components 1
yum install libaio -y &> /dev/null
fi
if [ -n "$mysql_source" ];then
mkdir ./$mysql_source 2>/dev/null && tar xf $mysql_source.tar.gz -C ./$mysql_source --strip-components 1 2>/dev/null
yum -y install cmake ncurses-devel openssl-devel gcc-c++ libaio-devel &> /dev/null
fi
}
# 修改配置文件
mysql_cnf () {
case $mycnf in
ab|AB|a|B)
cat >> /usr/local/mysql/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/master.err
log-bin=/usr/local/mysql/data/binlog
server-id=10
character_set_server=utf8mb4
EOF
;;
gtids|GTIDs|g|G)
cat >> /usr/local/mysql/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/master.err
log-bin=/usr/local/mysql/data/binlog
server-id=10
character_set_server=utf8mb4
gtid-mode=on
log-slave-updates=1
enforce-gtid-consistency
EOF
;;
esac
}
# MySQL设置
mysql_init () {
cd /usr/local/mysql
mkdir mysql-files
chown -R mysql.mysql mysql-files/
chmod 750 mysql-files/
chown -R mysql.mysql /usr/local/mysql
mysql_cnf
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &> /root/pass.txt
bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
cp support-files/mysql.server /etc/init.d/mysql
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
service mysql start
pass=`awk 'NR==7{print $NF}' /root/pass.txt`
mysqladmin -uroot password '123' -p$pass
mysql -e "flush privileges;" -uroot -p123
mysql_secure_installation <<EOF
123
No
No
Y
Y
Y
Y
EOF
chkconfig --add mysql
chkconfig mysql on
}
# 安装glibc版本
sql_glibc () {
if [[ $mysql_glibc = *glibc* ]];then
mysql_init
fi
}
# 安装源码版
sql_source () {
if [[ $mysql_source = mysql* ]];then
cd ./$mysql_source
cmake . \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DMYSQL_TCP_PORT=3306 \\
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_PARTITION_STORAGE_ENGINE=1 \\
-DWITH_FEDERATED_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_MYISAM_STORAGE_ENGINE=1 \\
-DENABLED_LOCAL_INFILE=1 \\
-DEXTRA_CHARSETS=all \\
-DDEFAULT_CHARSET=utf8mb4 \\
-DDEFAULT_COLLATION=utf8mb4_general_ci \\
-DWITH_SSL=system \\
-DWITH_BOOST=boost
make && make install
mysql_init
fi
}
# 主调函数
main () {
read -p "解压的MySQL_glibc版本,如mysql-5.7.31-linux-glibc2.12-x86_64,:" mysql_glibc
read -p "解压的MySQL_源码包版本,如mysql-boost-5.7.30,:" mysql_source
read -p "需要搭建哪种主从复制,如ab | GTIDs | :" mycnf
mysql_install
sql_glibc
sql_source
}
main
MySQL5.6安装
使用脚本安装MySQL并初始化:
- MySQL在安全配置的时候设置密码
#!/bin/bash
read -p "请输入需要安装的MySQL源码包,如mysql-5.6.33:" mysql_source
mkdir ./$mysql_source && tar xf $mysql_source.tar.gz -C ./$mysql_source --strip-components 1
if !(id mysql &> /dev/null);then
useradd -r -s /sbin/nologin -M mysql
fi
[ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak
# 安装依赖
yum -y install cmake bison-devel ncurses-devel libaio-devel
# 配置+编译安装
cd $mysql_source/
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DSYSCONFDIR=/etc \\
-DENABLED_LOCAL_INFILE=1 \\
-DWITH_PARTITION_STORAGE_ENGINE=1 \\
-DEXTRA_CHARSETS=all \\
-DDEFAULT_CHARSET=utf8mb4 \\
-DDEFAULT_COLLATION=utf8mb4_general_ci \\
-DWITH_SSL=bundled
make && make install
# 将MySQL的配置文件拷贝一份到/etc目录下
if [ -d /usr/local/mysql ];then
cd /usr/local/mysql
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
# 授权并初始化MySQL
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
fi
#安全配置服务、自启动和环境变量
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
service mysqld start
chkconfig --add mysqld
chkconfig mysqld on
# 对MySQL进行安全配置,删除匿名用户,利用expect自动交互套件执行
if ! (rpm -q expect &> /dev/null);then
yum -y install expect
fi
/usr/bin/expect <<-EOF
set timeout 20
spawn mysql_secure_installation
expect {
"enter for none" { send "\\r"; exp_continue }
" password:" { send "123\\r"; exp_continue }
"Y/n" { send "Y\\r"; exp_continue }
"Cleaning up" { send "\\r"}
}
expect eof
EOF
以上是关于MySQL安装脚本的主要内容,如果未能解决你的问题,请参考以下文章