MySQL的编译安装及多实例配置总结

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL的编译安装及多实例配置总结相关的知识,希望对你有一定的参考价值。

编译安装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用户

[[email protected] ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql

2.为MySQL用户添加家目录

[[email protected] ~]# chmod 700 /data/mysql
[[email protected] ~]# chown mysql.mysql /data/mysql

三、编译安装MySQL

1.解压MySQL安装包

[[email protected] ~]# tar -xf mariadb-10.2.23.tar.gz

2.编译安装MySQL

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

[[email protected] ~]# 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

[[email protected] mariadb-10.2.23]# make && make install

3.创建数据库

[[email protected] mariadb-10.2.23]# cd /app/mysql/
[[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MariaDB/MySQL system tables in ‘/data/mysql‘ ...
OK

4.放置配置文件并修改

[ro[email protected] mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[[email protected] mysql]# sed -i ‘/\[mysqld\]/adatadir=/data/mysql‘ /etc/mysql/my.cnf 

5.配置服务启动脚本

[[email protected] mysql]# chkconfig --add mysqld

6.启动服务

[[email protected] mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

7.为MySQL添加环境变量

[[email protected] mysql]# echo ‘PATH=/app/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] mysql]# . /etc/profile.d/mysql.sh

MySQL的多实例环境搭建

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

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

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

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

[[email protected] /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3306/data
[[email protected] /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3307/data

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

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

[[email protected] /]# cp /etc/my.cnf /mysql/3306/etc/my.cnf 
[[email protected] /]# 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

[[email protected] /]# cp /mysql/3306/etc/my.cnf /mysql/3307/etc/ 
[[email protected] /]# 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目录下,并加以修改

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

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

[[email protected]lhost /]# chmod +x /mysql/3306/bin/mysqld #为服务脚本添加执行权限
[[email protected] /]# chmod +x /mysql/3307/bin/mysqld 

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

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

4.启动实例1,2

[[email protected] /]# /mysql/3306/bin/mysqld start
Starting MySQL...
[[email protected] /]# /mysql/3307/bin/mysqld start
Starting MySQL...

[[email protected] /]# ss -tnl | grep 3306
LISTEN     0      80          :::3306                    :::*                  
[[email protected] /]# ss -tnl | grep 3307
LISTEN     0      80          :::3307                    :::*          

5.连接数据库

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

[[email protected] /]# 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=""中

[[email protected] /]# /mysql/3306/bin/mysqld stop
Stoping MySQL...
Enter password: 

以上是关于MySQL的编译安装及多实例配置总结的主要内容,如果未能解决你的问题,请参考以下文章

cmake安装mysql及多实例配置方法

MariaDB三种方法安装及多实例实现

MySQL第01课- CentOS + 单实例MySql编译安装总结

mysql安装与多实例(详解)

CentOS6.8下MySQL5.6.40基于GTID主从及多线程复制

Mysql DBA 高级运维学习笔记-mysql双主及多主同步过程