shell脚本一键部署MySQL数据库

安装环境:

CentOS 7
mysql-5.7.17.tar.gz
boost_1_59_0.tar.gz


脚本描述:

#!/bin/bash
#一键部署MySQL编译安装
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 &> /dev/null

#检查/opt目录下是否有指定安装包
if [ -e /opt/mysql-5.7.17.tar.gz ];then
  continue
  if [ -e /opt/boost_1_59_0.tar.gz ];then
    continue
  else
    echo -e '\033[35m /opt目录中没有安装包[boost_1_59_0.tar.gz]\033[0m'
  exit
  fi
else
  echo -e '\033[35m /opt目录中没有安装包[mysql-5.7.17.tar.gz]\033[0m'
  exit
fi

#检查依赖包是否安装
if rpm -q gcc gcc-c++ ncurses ncurses-devel bison cmake &> /dev/null
then
   continue
else
   if yum install -y gcc gcc-c++ ncurses ncurses-devel bison cmake &> /dev/null
   then
      continue
   else
      echo -e "\033[31;5m 依赖包安装失败,请检查yum源是否正常! \033[0m"
      exit
   fi
fi

#创建新用户编译安装
cd /opt
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost
useradd -M -s /sbin/nologin mysql

#安装模块
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

make && make install

#修改mysql配置文件
sed -i 'd' /etc/my.cnf
echo '[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[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

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 &> /dev/null

if netstat -napt |grep mysql &> /dev/null ;then
    mysqladmin -u root -p password "abc123"
    echo -e '\033[35m MySQL数据库已部署完成!修改密码成功!请登录测试!\033[0m'
else
    echo -e '\033[35m MySQL端口未打开!请检查相关配置!\033[0m'
fi 

后续操作:

#修改mysql 的登录密码
mysqladmin -u root -p password "abc123" 	#给root账号设置密码为abc123,提示输入的是原始密码(为空)

#授权远程登录
mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限

show databases;			#查看当前已有的数据库

出现问题:

[root@localhost ~]# mysql -uroot -p
bash: mysql: 未找到命令...

解决方案:

echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile	
source /etc/profile
上一篇:CSS制作网页布局


下一篇:css快速入门(上)