编译安装MySQL+多实例

编译安装MySQL

编译环境 MySQL版本
CentOS 7.6 mariadb-10.2.23

一、安装编译所需要的包

yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssldevel libevent-devel libaio-devel -y 

二、添加MySQL用户及其家目录

1.添加MySQL用户

[root@localhost ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql

2.为MySQL用户添加家目录

[root@localhost ~]# chmod 700 /data/mysql
[root@localhost ~]# chown mysql.mysql /data/mysql

三、编译安装MySQL

1.解压MySQL安装包

[root@localhost ~]# tar -xf mariadb-10.2.23.tar.gz

2.编译安装MySQL

此处需要注意如果编译时中间如果出现缺包的错误,需要将此目录下的CMakeCache.txt删除后重新进行编译。

[root@localhost ~]# cd mariadb-10.2.23
[root@localhost mariadb-10.2.23]# cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/mysql \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

[root@localhost mariadb-10.2.23]# make && make install

3.创建数据库

[root@localhost mariadb-10.2.23]# cd /app/mysql/
[root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
OK

4.放置配置文件并修改

[root@localhost mysql]# mkdir /etc/mysql        #c创建MySQL配置文件目录
[root@localhost mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[root@localhost mysql]# sed -i '/\[mysqld\]/adatadir=/data/mysql' /etc/mysql/my.cnf 

5.配置服务启动脚本

[root@localhost mysql]# cp support-files/mysql.service /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld

6.启动服务

[root@localhost mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

7.为MySQL添加环境变量

[root@localhost mysql]# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost mysql]# . /etc/profile.d/mysql.sh

MySQL的多实例环境搭建

在测试环境中通常会需要在一台主机上搭建多个版本的MySQL,此处演示如何搭建多实例的MySQL。

一、为每个实例创建各自的目录并赋予权限(此处以创建2个实例为例)

[root@localhost /]# mkdir -pv /mysql/{3306,3307}/{data,etc,socket,bin,log,pid}
[root@localhost ~]# chown -R mysql.mysql /mysql
[root@localhost ~]# tree /mysql                 #查看下目录结构
/mysql                                    
├── 3306
│   ├── bin
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
└── 3307
    ├── bin
    ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket

二、为每个实例创建数据库文件

[root@localhost /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3306/data
[root@localhost /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3307/data

三、为每个实例创建配置文件

配置文件可以参考/etc/my.cnf进行修改

[root@localhost /]# cp /etc/my.cnf /mysql/3306/etc/my.cnf 
[root@localhost /]# vim /mysql/3306/etc/my.cnf                  #修改my.cnf,写入配置
[mysqld]                                                        #注意需要将#!includedir /etc/my.cnf.d注释
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid

[root@localhost /]# cp /mysql/3306/etc/my.cnf /mysql/3307/etc/ 
[root@localhost /]# sed -i 's/3306/3307/' /mysql/3307/etc/my.cnf 

四、为实例添加服务脚本

1.此为事先准备好的服务脚本

#!/bin/bash

port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
    else
      printf "MySQL is running...\n"
      exit
    fi
}

function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

2.将服务启动脚本存放至各实例的bin目录下,并加以修改

[root@localhost /]# vim /mysql/3306/bin/mysqld      #对端口和cmd_path进行修改
port=3306
cmd_path="/app/mysql/bin"

[root@localhost /]# vim /mysql/3307/bin/mysqld      #对端口和cmd_path进行修改
port=3307
cmd_path="/app/mysql/bin"

[root@localhost /]# chmod +x /mysql/3306/bin/mysqld #为服务脚本添加执行权限
[root@localhost /]# chmod +x /mysql/3307/bin/mysqld 

3.关闭编译安装的MySQL服务

[root@localhost /]# ss -tnl |grep 3306
LISTEN     0      80          :::3306                    :::*       
[root@localhost /]# service mysqld stop
Stopping mysqld (via systemctl):                           [  OK  ]

4.启动实例1,2

[root@localhost /]# /mysql/3306/bin/mysqld start
Starting MySQL...
[root@localhost /]# /mysql/3307/bin/mysqld start
Starting MySQL...

[root@localhost /]# ss -tnl | grep 3306
LISTEN     0      80          :::3306                    :::*                  
[root@localhost /]# ss -tnl | grep 3307
LISTEN     0      80          :::3307                    :::*          

5.连接数据库

客户端在连接多实例的MySQL时,需要指定端口号和socket文件路径否则报错。

[root@localhost /]# mysql -uroot -p -P3306 -S /mysql/3306/socket/mysql.sock 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.23-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

6.关闭多实例MySQL服务

多实例的MySQL服务在关闭时需要输入MySQL管理员的密码,也可以将管理员密码存放在配置文件的变量mysql_pwd=""中

[root@localhost /]# /mysql/3306/bin/mysqld stop
Stoping MySQL...
Enter password: