# MYSQL5.X源码安装脚本



**MYSQL源码安装脚本**

> **系统环境：CentOS 7.4**

```bash
#!/usr/bin/env bash
#2020-3-12 10:31:01
#BY：NBPASS
#Auto Install MYSQL Server
 
source /etc/rc.d/init.d/functions
 
#Define MYSQL path variables
Boost_URL=https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0
Boost_File=boost_1_59_0.tar.gz
 
#Define MYSQL path variables
MYSQL_URL=https://dev.mysql.com/get/Downloads/MySQL-5.7
MYSQL_FILES=mysql-5.7.29.tar.gz
MYSQL_FILES_DIR=mysql-5.7.29
MYSQL_PREFIX=/usr/local/mysql
MYSQL_DIR=/data/mysql
MYSQL_USER=mysql
CPU_NUM=$(cat /proc/cpuinfo |grep processor |wc -l)
 
#Install Package
yum -y install ncurses-devel perl perl-devel cmake wget gcc gcc-c++ bison* autoconf openssl-devel openssl
 
#Install Boost
wget -c ${Boost_URL}/${Boost_File}
tar zxf ${Boost_File} -C /usr/local/
 
#Install MYSQL
if [ ! -d ${MYSQL_PREFIX} ];then
wget -c ${MYSQL_URL}/${MYSQL_FILES}
tar zxf ${MYSQL_FILES}
cd ${MYSQL_FILES_DIR}
cmake . -DCMAKE_INSTALL_PREFIX=${MYSQL_PREFIX} \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=${MYSQL_DIR} \
-DSYSCONFDIR=/etc \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_TCP_PORT=3306 \
-DWITH_READLINE=1 \
-DMYSQL_USER=${MYSQL_USER} \
-DWITH_SSL=yes
if [ $? -eq 0 ];then
        make -j${CPU_NUM} && make install
        action "The MYSQL Install Sussess..." /bin/true
else
        action "The MYSQL Install Failed..." /bin/false 
        exit 1
fi
else
        echo -e "\033[31mThe MYSQL already Install...\033[0m"
        exit 1
fi
 
#Mysql config
useradd -s /sbin/nlogin mysql >/dev/null 2>&1
mkdir -p ${MYSQL_DIR}
chown -R ${MYSQL_USER}.${MYSQL_USER} ${MYSQL_DIR}
cat >/etc/my.cnf << EOF
[mysqld]
#数据存储目录
datadir = ${MYSQL_DIR}
#socket通信文件
socket = /tmp/mysql.sock
#使用mysql用户启动
user = ${MYSQL_USER}
#MYSQL服务运行的端口号
port = 3306
#开启bin-log日志
log-bin = mysql-bin
#MYSQL服务ID号
server-id = 1
#定义error错误文件
log-error = ${MYSQL_DIR}/mysqld.log
#PID文件路径
pid-file = mysqld.pid
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#设置字符集为utf8
character-set-server = utf8
[client]
default-character-set = utf8
port = 3306
socket = /tmp/mysql.sock
[mysql]
default-character-set = utf8
EOF
 
#Initialization Mysql
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/mysql/ --basedir=/usr/local/mysql
ln -sf ${MYSQL_PREFIX}/bin/* /usr/bin
cp ${MYSQL_PREFIX}/support-files/mysql.server /etc/init.d/mysqld
chmod o+x /etc/init.d/mysqld
/etc/init.d/mysqld start

```
