脚本用法:chmod 755 install-mysql.sh && sh install-mysql.sh 5.7.22 (后面跟的是你需要的版本号,需要什么版本就写什么版本),我这里安装的5.7.22
#!/bin/sh
# mysql 任何版本全程自动化源码编译安装
# 用法:sh -x install-mysql.sh 5.7.22 后面跟的是你需要的版本号,需要什么版本就写什么版本!
version=$1
usage(){
echo "usage: $0 version"
}
if [ $# -ne 1 ]
then
usage
exit -1
fi
if [ -s /etc/my.cnf ];then
rm -rf /etc/my.cnf
fi
echo "==================开始安装 mysql ===================="
yum install -y ncurses gcc gcc-c++ ncurses ncurses-devel openssl openssl-devel libtool* cmake
mkdir -p /data/mysql
if [ 'grep "mysql" /etc/passwd | wc -l' ]; then
echo "adding user mysql"
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
else
echo "mysql user 已存在!"
fi
echo "========================下载mysql===================="
if [ ! -f mysql-${version}.tar.gz ]
then
wget http://dev.mysql.com/get/Downloads/MySQL-${version}/mysql-${version}.tar.gz
fi
echo "========================解压mysql安装包================"
tar -xvf mysql-${version}.tar.gz
cd mysql-${version}
echo "======================开始配置mysql===================="
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/var/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DMYSQL_USER=mysql
if [ $? -ne 0 ];then
echo "配置失败,请检查!!"
exit 1
fi
echo "make mysql"
make
if [ $? -ne 0 ];then
echo "make 失败,请检查!!"
exit 1
fi
make install
chown -R mysql:mysql /usr/local/mysql
chown -R mysql.mysql /data/mysql/
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
chgrp -R mysql /usr/local/mysql/
cp -f ./support-files/my-large.cnf /etc/my.cnf
sed -i 's#^thread_concurrency = 8#& \ndatadir = /data/mysql#g' /etc/my.cnf
cp ./support-files/mysql.server /etc/rc.d/init.d/mysql
chmod 755 /etc/init.d/mysql
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
echo "mysql 启动中---"
/usr/local/mysql/bin/mysqld --user=mysql
if [ $? -ne 0 ];then
echo "mysql 启动失败,请检查配置!"
else
echo "mysql 启动成功l!!"
fi
注意:安装高版本之前需要下载解压boost软件,因为需要boost库的支持,不然后报错
1、在/usr/local下创建一个boost的文件夹,命令如下:
mkdir -p /usr/local/boost
2、进入这个新创建的文件夹然后下载boost
下载链接:wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz3进行解压就可以了,命令如下:
tar -xvzf boost_1_59_0.tar.gz
开始安装:
部署配置完成
安装完成后,可以登陆mysql数据库并进行一些简单的sql操作,如下图所示: