Shell脚本一键部署——源码编译安装MySQL及自动补全工具
编译安装MySQL
1、软件包
Mysql安装包
将安装包拖至/opt目录下,编辑一个脚本文件,将以下内容复制进去,然后source或者. 执行脚本,等脚本执行完成,即可使用mysql -u root -p点击回车即可进入。
2、MySQL及自动补全工具一键部署脚本(使用source或./执行脚本!)
#!/bin/bash
echo -e "----正在验证当前为仅主机还是NAT模式----"
ping -c 2 -w 2 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "检测当前为NAT模式,为您配置在线yum源"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "在线源已配置完成"
else
echo -e "检测当前为仅主机模式,为您配置本地yum源"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "检测当前为仅主机模式,但光盘未挂载!"
else
echo -e "本地yum源已配置完成"
fi
fi
#编译安装mysqld服务
#将安装mysql所需软件包传到/opt目录下,并解压
cd /opt
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz
#安装环境依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake
if [ $? -eq 0 ];then
echo -e "环境依赖包安装完成"
fi
#配置软件模块
mv boost_1_59_0 /usr/local/boost
cd /opt/mysql-5.7.17/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1
if [ $? -eq 0 ];then
echo "模块配置完成"
fi
#编译安装
make -j4 && make install
if [ $? -eq 0 ];then
echo "编译安装完成"
fi
#创建mysql用户
useradd -M -s /sbin/nologin mysql
#修改mysql配置文件
echo '[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
auto-rehash
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
#更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
#设置路径环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
#初始化数据库
cd /usr/local/mysql/bin
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
#添加mysql系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
netstat -anpt | grep 3306
rpm -q expect
if [ $? -eq 1 ];then
yum -y install expect
fi
source /etc/profile
#修改mysql登录密码
/usr/bin/expect <<EOF
spawn mysqladmin -u root -p password "123456"
expect "Enter password:"
send "\r"
expect eof
EOF
#授权远程登录
/usr/bin/expect <<EOF
spawn mysql -u root -p
expect "Enter password:" {send "123456\r"}
expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by '123456';\r"}
expect "mysql>" {send "show databases;\r"}
expect "mysql>" {send "quit\r"}
expect eof
EOF
source /etc/profile
echo "----mysql安装完成----"
#在线源安装
yum -y install epel-release
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-develdb4 -devel libpcap-devel xz-devel gcc
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
mkdir ~/.pip/
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
yum -y install libffi-devel zlib* python-devel openssl-devel
tar -zxvf Python-3.9.0.tgz
cd Python-3.9.0/
./configure --prefix=/usr/local/python --with-ssl
make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/
echo 'export PATH=/usr/local/python/bin:$PATH' >> /etc/profile
source /etc/profile
pip3 install mycli
if [ $? -eq 0 ];then
echo -e "-----mycli自动补全功能已经安装完成-----"
fi
#mycli -u root -p <密码>