[root@localhost conf]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost conf]# uname -r
3.10.0-957.el7.x86_64
[root@localhost conf]#
#查看是否安装过mysql和mariadb
rpm -qa | grep mysql
rpm -qa | grep mariadb
#卸载历史版本
rpm -e --nodeps mariadb-libs
https://downloads.mysql.com/archives/community/
wget -c https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.30.tar.gz
wget -c https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.30.tar.gz
MySQL从5.7版本之后,boost是必须的,这里需要解压到相应的安装目录,后面Cmake会用到
mkdir -p /usr/local/mysql-boost
tar xvf mysql-boost-5.7.30.tar.gz -C /usr/local/mysql-boost
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel libaio autoconf
groupadd mysql
useradd -M -s /sbin/nologin -r -g mysql mysql
#注释 -M 不创建家目录 -s 指定默认登录shell -r创建系统账户 -g指定用户登录组的GID或组名
解压下载的源文件
tar xvf mysql-5.7.30.tar.gz
cd mysql-5.7.30
规划安装目录
#安装目录
mkdir -p /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
#数据目录
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
进入解压后的源文件目录
cd mysql-5.7.30
cmake:
cmake -DMYSQL_USER=mysql \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_BOOST=/usr/local/mysql-boost/mysql-5.7.30/boost
注:-DWITH_BOOS参数就是第四步骤的解压后路径,注意要写到boost目录
说明:如果编译出错,没有通过,检测参数后重新运行配置,需要删除CMakeCache.txt文件
#make clean
#rm -f CMakeCache.txt
参数从哪里找:
http://www.mysql.com→→Documentation→→选择对应的版本(5.7)Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options
直接点击url: https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
编译安装
make -j 4&&make install
/etc/my.cnf
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysqld.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
character-set-server=utf8
#把mysql的bin路径加入PATH
echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
#让PATH变量修改立即生效
source /etc/profile
#设置服务自动启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
#设置服务自动启动
chkconfig mysql on
由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
ln -s /usr/local/mysql-5.7.30/bin/mysql /usr/bin
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
/etc/init.d/mysql start
#查看临时密码
grep password /data/mysql/mysql.err
#使用临时密码登录后修改密码
SET PASSWORD = PASSWORD('123456');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
#!/bin/bash
#auto install MYSQL
#by MR.xu 2020-07
#MYSQL define path variable
#auto install mysql
#mysql define path variable
M_FILES=mysql-5.7.30.tar.gz
M_FILES_DIR=mysql-5.7.30
M_URL=https://cdn.mysql.com/archives/mysql-5.7/
M_PREFIX=/usr/local/mysql
BOOST_FILES=mysql-boost-5.7.30.tar.gz
#安装环境的检查
##卸载系统自带的mysql|mariadb
rpm -qa | grep mysql;
if [ $? -eq 0 ];then
echo -e "\033[32mMySQL has been installed on the system and will be uninstalled!!!\033[0m"
mv /etc/my.cnf /etc/my.cnf`date +%Y-%m-%d-%H-%M-%S`;
mv /etc/my.cnf.d /etc/my.cnf.d`date +%Y-%m-%d-%H-%M-%S`;
rpm -e --nodeps mariadb-libs;
else
echo -e "\033[32mMySQL is not installed in the system!!\033[0m"
rpm -qa | grep mariadb;
if [ $? -eq 0 ];then
mv /etc/my.cnf /etc/my.cnf`date +%Y-%m-%d-%H-%M-%S`;
mv /etc/my.cnf.d /etc/my.cnf.d`date +%Y-%m-%d-%H-%M-%S`;
echo -e "\033[32mMariadb has been installed on the system and will be uninstalled!!!\033[0m"
rpm -e --nodeps mariadb-libs;
else
echo -e "\033[32mMariadb is not installed in the system!!\033[0m"
fi
fi
#安装mysql-boost
#安装路径/usr/local/mysql-boost,CMAKE的-DWITH_BOOS参数要用到解压后路径,注意要写到boost目录
wget -c $M_URL/$BOOST_FILES&&mkdir -p /usr/local/mysql-boost&&tar xvf $BOOST_FILES -C /usr/local/mysql-boost
if [ $? -eq 0 ];then
echo -e "\033[32mThe mysql-boost path is /usr/local/mysql-boost\033[0m"
else
echo -e "\033[32mThe mysql-boost path is faild to create,please check it!\033[0m"
exit
fi
#安装依赖包
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel libaio autoconf
#添加mysql用户和用户组
groups mysql;
if [ $? -eq 0 ];then
echo -e "\033[32mThe MySQL user already exists! \033[0m"
else
groupadd mysql&&useradd -M -s /sbin/nologin -r -g mysql mysql
echo -e "\033[32mThe MySQL user created successfully \033[0m"
fi
if [ $? -eq 0 ];then
echo -e "\033[32m添加mysql用户和用户组成功\033[0m"
else
echo -e "\033[32m添加mysql用户和用户组faild,please check it!\033[0m"
exit
fi
#创建安装目录和数据目录
mkdir -p $M_PREFIX&&chown -R mysql:mysql $M_PREFIX;
mkdir -p /data/mysql&&chown -R mysql:mysql /data/mysql;
#下载mysql安装包并编译
if [ $? -eq 0 ];then
wget -c $M_URL/$M_FILES &&tar -zxvf $M_FILES &&cd $M_FILES_DIR ;cmake -DMYSQL_USER=mysql \
-DCMAKE_INSTALL_PREFIX=$M_PREFIX \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_BOOST=/usr/local/mysql-boost/$M_FILES_DIR/boost
if [ $? -eq 0 ];then
echo -e "\033[32mThe $M_FILES_DIR Cmake successfully!\033[0m"
echo -e "\033[32mStart to make install. It will be finished in about half an hour. Please wait patiently!\033[0m"
make&&make install;
echo -e "\033[32mThe $M_FILES_DIR sever install successfully!\033[0m"
##开始配置mysql的my.cnf
cat >/etc/my.cnf <<EOF
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysqld.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
character-set-server=utf8
EOF
##添加path路径,让系统能够读到mysql命令
echo "export PATH=$PATH:$M_PREFIX/bin">>/etc/profile&&source /etc/profile;
##Linux启动时,会寻找/etc/init.d/目录中的服务启动脚本
cp $M_PREFIX/support-files/mysql.server /etc/init.d/mysql&&chmod +x /etc/init.d/mysql
##创建软连接做成mysql服务
ln -s $M_PREFIX/bin/mysql /usr/bin
##修改权限并初始化数据库
chown -R mysql:mysql $M_PREFIX;
$M_PREFIX/bin/mysqld --initialize-insecure --user=mysql --basedir=$M_PREFIX --datadir=/data/mysql
##启动数据库
/etc/init.d/mysql start;
if [ $? -eq 0 ];then
ps -ef|grep mysql;
echo -e "\033[32mThe mysql start successfully!\033[0m"
echo -e "\033[32mView mysql temporary password!\033[0m"
grep password /data/mysql/mysql.err
else
echo -e "\033[32mThe mysql start faild!\033[0m"
fi
else
echo -e "\033[32mThe $M_FILES_DIR sever install failed,please check...!\033[0m"
#CMake失败的情况下要再次CMake需要删除CMakeCache.txt,这里做了备份防止下次跑脚本报错
mv CMakeCache.txt CMakeCache.txt`date +%Y-%m-%d-%H-%M-%S`
exit
fi
fi