一、操作步骤

  1. 将安装包mysql-boost-5.7.20.tar.gz拖至/opt目录下
  2. 编辑一个脚本文件,将以下脚本内容复制进去,然后source或者. 执行脚本,等脚本执行完成。
  3. 使用命令 mysqladmin -u root -p password 123456 修改passwd,123456为修改的passwd,也可自己设置。
  4. 使用mysql -u root -p点击回车,输入passwd即可进入数据库。

二、脚本文件内容

#!/bin/bash
#编译安装Mysql服务
#使用脚本前注意事项:
#1、请设置好yum源仓库
#2、请在/opt目录下放入mysql-boost-5.7.20.tar.gz安装包

#编写安装mysql的函数
MySQL() {

#关闭系统防火墙和安全机制
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

#安装环境依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

#创建运行用户
useradd -M -s /sbin/nologin  mysql

#编译安装
cd /opt
tar xzvf mysql-boost-5.7.20.tar.gz

cd /opt/mysql-5.7.20/
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=boost \
-DWITH_SYSTEMD=1

#编译及安装
make && make install

#修改mysql 配置文件
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

#更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

#设置路径环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib">> /etc/profile   
source /etc/profile

#初始化数据库
cd /usr/local/mysql/bin/
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

#添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ 

systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
}

#主体代码
ls /opt | egrep "^mysql-boost-5.7.20.tar.gz$"
if [ $? -eq 0 ];then
  #调用函数安装mysql
  MySQL
  echo "数据库安装完毕,请设定root用户passwd。"
  echo "例如:输入“mysqladmin -u root -p password "123456" ” + 回车;回车"

else
  echo -e "请将安装包mysql-boost-5.7.20.tar.gz放入/opt目录下"
  #如果没有就终止
  break
fi