0
点赞
收藏
分享

微信扫一扫

安装mysql多实例

其生 2022-07-12 阅读 97

前提:如果是centos,最⼩化安装系统,可以安装如下的基础环境依赖包,【可选】

# yum clean all
# yum -y update  
# 上面两行不执行也行吧
yum -y install gcc-c++ gd libxml2-devel libjpeg-devel libpngdevel net-snmp-devel wget telnet vim zip unzip
yum -y install curl-devel libxslt-devel pcre-devel libjpeg libpng libcurl4-openssl-dev
yum -y install libcurl-devel libcurl freetype-config freetype freetype-devel unixODBC libxslt
yum -y install gcc automake autoconf libtool openssl-devel
yum -y install perl-devel perl-ExtUtils-Embed *libnuma* screen
yum -y install cmake ncurses-devel.x86_64 openldapdevel.x86_64 lrzsz openssh-clients gcc-g77 bison
yum -y install libmcrypt libmcrypt-devel mhash mhash-devel bzip2 bzip2-devel
yum -y install ntpdate rsync svn patch iptables iptablesservices
yum -y install libevent libevent-devel cyrus-sasl cyrus-sasldevel
yum -y install gd-devel libmemcached-devel memcached git libssl-devel libyaml-devel auto make
yum -y install gcc gcc-c++ make autoconf automake ncursesdevel bison ncurses cmake libaio libaio-devel boost
yu
m -y groupinstall "Server Platform Development" "Development tools"
yum -y groupinstall "Development tools"

⼆进制安装mysql mysql

# 1. 获取⼆进制代码包
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
# 2. 系统基础环境依赖安装
yum install ncurses-devel libaio-devel gcc make cmake -y
# 3.创建系统⽤户,之前创建过了,就⽆须执⾏了
[root@tech_master01 ~]# groupadd mysql
groupadd: group 'mysql' already exists
[root@tech_master01 ~]# useradd -g mysql mysql
useradd: user 'mysql' already exists
[root@tech_master01 ~]#
# 4.创建多实例的独⽴⽬录
[root@tech_master01 ~]# mkdir -p /my_mysql/{3306,3307}
# 5.解压缩mysql⼆进制压缩包
[root@tech_master01 ~]# tar -xzvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz -C /application/
[root@tech_master01 ~]# yum -y install tree
[root@tech_master01 ~]# tree -L 1 /my_mysql/
/my_mysql/
├── 3306
├── 3307

停⽌编译的mysql mysql

# 1. 检查、且停掉之前编译安装的mysql
[root@tech_master01 ~]# netstat -tunlp|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      6707/mysqld         
[root@tech_master01 ~]# systemctl stop mysqld
[root@tech_master01 ~]# netstat -tunlp|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      6707/mysqld         
[root@tech_master01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@tech_master01 ~]# netstat -tunlp|grep mysql
# 2. 禁⽌开机⾃启
[root@tech_master01 ~]# systemctl is-enabled mysqld
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld --level=5
disabled
[root@tech_master01 ~]# chkconfig --list mysqld

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

服务 mysqld 支持 chkconfig,但它未在任何运行级别中被引用(请运行“chkconfig --add mysqld”)

准备二进制mysql运行所需的环境

[root@tech_master01 ~]# ls /application/ -lth
总用量 0
#  二进制解压的mysql
drwxr-xr-x 13 root  root  191 7月   9 14:53 mysql-5.6.40-linux-glibc2.12-x86_64
#  之前安装的mysql
drwxr-xr-x 14 mysql mysql 234 6月  29 16:04 mysql-5.6.40
#  之前安装的mysql的软链接
lrwxrwxrwx  1 mysql mysql  26 6月  27 18:34 mysql -> /application/mysql-5.6.40/
# 1. 准备mysql多实例的各个配置文件
[root@tech_master01 ~]# cd /my_mysql/3306/
[root@tech_master01 3306]# vim my.cnf
[client]
port=3306
socket=/my_mysql/3306/mysql.sock

[mysqld]
port=3306
socket=/my_mysql/3306/mysql.sock
basedir=/application/mysql-5.6.40-linux-glibc2.12-x86_64/
datadir=/my_mysql/3306/data
log-bin=/my_mysql/3306/mysql-bin
server-id=1

[mysqld_safe]
log-error=/my_mysql/3306/mysql_3306_error.log
pid-file=/my_mysql/3306/mysqld_3306.pid
# 3307配置文件同理,注:修改完端口号还要改server-id成不一样的
# 2. 准备各个启停管理脚本

[root@tech_master01 3306]# vim mysqld_3306
port=3306
mysql_user="mysql"
Cmdpath="/application/mysql-5.6.40-linux-glibc2.12-x86_64/bin"
mysql_sock="/my_mysql/${port}/mysql.sock"
mysqld_pid_file_path=/my_mysql/${port}/mysqld_${port}.pid
start(){
    if [ ! -e "$mysql_sock" ];then
        printf "Starting MySQL...\n"
        /bin/sh ${Cmdpath}/mysqld_safe --defaults-Afile=/my_mysql/${port}/my.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null & 
    sleep 3
    else
        printf "MySQL is running...\n"
        exit 1
    fi
}

stop(){
    if [ ! -e "$mysql_sock" ];then
        printf "MySQL is stopped...\n"
        exit 1
    else
        printf "Stoping MySQL...\n"
        mysqld_pid=`cat "$mysqld_pid_file_path"`
        if (kill -0 $mysqld_pid 2>/dev/null)
        then
            kill $mysqld_pid
            sleep 2
        fi
    fi
}

restart(){
    printf "Restarting MySQL...\n"
    stop
    sleep 2

    start
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        printf "Usage: /my_mysql/${port}/mysql{start|stop|restart}\n"
esac
# 3307只需要改一下端口变量=3307即可
# 赋予可执行权限
[root@tech_master01 3306]# chmod +x mysqld_3306
#### 用户、组授权
[root@tech_master01 3306]# chown -R mysql.mysql /my_mysql/

PATH配置

[root@tech_master01 3306]# vim /etc/profile
# 尾部加下面一句
export PATH=/application/mysql-5.6.40-linux-glibc2.12-x86_64/bin:$PATH

创建多个实例对应的数据目录

[root@tech_master01 ~]# mkdir /my_mysql/330{6,7}/data -p
[root@tech_master01 ~]# tree /my_mysql/
/my_mysql/
├── 3306
│   ├── data
│   ├── my.cnf
│   └── mysqld_3306
└── 3307
    ├── data
    ├── my.cnf
    └── mysqld_3307

4 directories, 4 files

见证mysql的多实例初始化

# 1.初始化3306的数据
# 初始化前data目录为空
[root@tech_master01 ~]# ll /my_mysql/3306/data/
总用量 0
[root@tech_master01 ~]# /application/mysql-5.6.40-linux-glibc2.12- x86_64/scripts/mysql_install_db --defaults-file=/my_mysql/3306/my.cnf --basedir=/application/mysql-5.6.40-linux-glibc2.12-x86_64/ --datadir=/my_mysql/3306/data/ --user=mysql
# 出现两个OK后,数据正常
# 此时data目录就有初始数据了
[root@tech_master01 ~]# ll /my_mysql/3306/data/
总用量 110600
-rw-rw---- 1 mysql mysql 12582912 7月   9 18:03 ibdata1
-rw-rw---- 1 mysql mysql 50331648 7月   9 18:03 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 7月   9 18:03 ib_logfile1
drwx------ 2 mysql mysql     4096 7月   9 18:03 mysql
drwx------ 2 mysql mysql     4096 7月   9 18:03 performance_schema
drwx------ 2 mysql mysql        6 7月   9 18:03 test
# 同理生成3307的初始数据
[root@tech_master01 ~]# /application/mysql-5.6.40-linux-glibc2.12-x86_64/scripts/mysql_install_db --defaults-file=/my_mysql/3307/my.cnf --basedir=/application/mysql-5.6.40-linux-glibc2.12-x86_64/ --datadir=/my_mysql/3307/data/ --user=mysql

分别启动mysql的多实例

# 1.检查一下是否还有mysql在运行,即看看有没有3306端口正在运行
[root@tech_master01 ~]# netstat -lntup 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6385/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      6385/sshd           
udp        0      0 127.0.0.1:323           0.0.0.0:*                           6026/chronyd        
udp6       0      0 ::1:323                 :::*                                6026/chronyd
# 2. 启动发现报错
[root@tech_master01 ~]# /my_mysql/3306/mysqld_3306 start
Starting MySQL...
220710 12:24:14 mysqld_safe error: log-error set to '/my_mysql/3306/mysql_3306_error.log', however file don't exists. Create writable for user 'mysql'.
# 手动创建/my_mysql/3306/mysql_3306_error.log文件即可
[root@tech_master01 ~]# touch /my_mysql/3306/mysql_3306_error.log
[root@tech_master01 ~]# /my_mysql/3306/mysqld_3306 start
[root@tech_master01 ~]# netstat -lntup|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      10063/mysqld
# 对3307重复以上操作,查看端口如下,则表示成功:
[root@tech_master01 ~]# netstat -lntup|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      10350/mysqld        
tcp6       0      0 :::3307                 :::*                    LISTEN      10722/mysqld 

登录3306数据库

# 用如下命令登录,使用sock套接字文件登录
[root@tech_master01 ~]# mysql -S /my_mysql/3306/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

相关推荐

0 条评论