vlambda博客
学习文章列表

实战-多实例安装MySQL8.0.20

目标:安装两个实例分别3306端口和3307端口,启动并登录成功。

1.下载安装包

cd /opt

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

2.解压文件

tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

3.移动解压后的数据文件(创建mysql8.0.20文件夹)

mv /opt/mysql-8.0.20-linux-glibc2.12-x86_64/* /usr/local/mysql8.0.20/

4.创建mysql组

groupadd mysql

5.创建mysql用户并添加到mysql组

useradd -g mysql mysql

6.创建3306端口和3307端口实例data/logs/tmp目录

mkdir -p /data/mysql8.0.20/mysql3306/{data,logs,tmp}

mkdir -p /data/mysql8.0.20/mysql3307/{data,logs,tmp}

7.修改目录权限

chown -R mysql:mysql /data/mysql8.0.20/mysql3306/{data,logs,tmp}

chown -R mysql:mysql /data/mysql8.0.20/mysql3307/{data,logs,tmp}

chown -R mysql:mysql /usr/local/mysql8.0.20

8.创建3306端口和3307端口实例*.cnf(参数配置)文件

3306端口实例配置文件mysql3306.cnf放到/data/mysql8.0.20/mysql3306/tmp下,以下为基础参数

[mysqld]

port=3306

socket=/tmp/mysql3306.sock

basedir=/usr/local/mysql8.0.20

datadir=/data/mysql8.0.20/mysql3306/data

tmpdir=/data/mysql8.0.20/mysql3306/tmp

#error log

log-error=/data/mysql8.0.20/mysql3306/logs/mysql_error.log

log_error_verbosity=3

3307端口实例配置文件mysql3307.cnf放到/data/mysql8.0.20/mysql3307/tmp下,以下为基础参数

[mysqld]

port=3307

socket=/data/mysql8.0.20/mysql3307/tmp/mysql3307.sock

basedir=/usr/local/mysql8.0.20

datadir=/data/mysql8.0.20/mysql3307/data

tmpdir=/data/mysql8.0.20/mysql3307/tmp

#error log

log-error=/data/mysql8.0.20/mysql3307/logs/mysql_error.log

log_error_verbosity=3

3307端口实例配置文件

9.初始化数据库

9.1#有密码初始化

9.1.1初始化3306端口实例

/usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --user=mysql --initialize

9.1.2初始化3307端口实例

/usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3307/tmp/mysql3307.cnf --user=mysql --initialize

9.2#无密码初始化

9.2.1初始化3306端口实例

/usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --user=mysql --initialize-insecure

9.2.2初始化3307端口实例

/usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3307/tmp/mysql3307.cnf --user=mysql --initialize-insecure

10.查看密码(如果选择无密码初始化,可跳过此步)

10.1查看3306端口实例登录密码

cat /data/mysql8.0.20/mysql3306/logs/mysql_error.log

10.2查看3307端口实例登录密码

cat /data/mysql8.0.20/mysql3307/logs/mysql_error.log

11.启动数据库

11.1启动3306端口实例

[root@centos-7 ~]# /usr/local/mysql8.0.20/bin/mysqld_safe --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --user=mysql &

[1] 6649

[root@centos-7 ~]# 2020-07-13T06:27:00.146171Z mysqld_safe Logging to '/data/mysql8.0.20/mysql3306/logs/mysql_error.log'.

2020-07-13T06:27:00.209045Z mysqld_safe Starting mysqld daemon with databases from /data/mysql8.0.20/mysql3306/data

我们看下后台是否有这个进程

[root@centos-7 ~]# ps aux | grep mysql

root      6649  0.2  0.0 113620  1720 pts/0    S    14:26   0:00 /bin/sh /usr/local/mysql8.0.20/bin/mysqld_safe --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --user=mysql

mysql     7024  9.1 11.4 3027340 443460 pts/0  Sl   14:26   0:01 /usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --basedir=/usr/local/mysql8.0.20 --datadir=/data/mysql8.0.20/mysql3306/data --plugin-dir=/usr/local/mysql8.0.20/lib/plugin --user=mysql --log-error=/data/mysql8.0.20/mysql3306/logs/mysql_error.log --pid-file=centos-7.shared.pid --socket=/tmp/mysql3306.sock --port=3306

root      7141  0.0  0.0 112824   980 pts/0    S+   14:27   0:00 grep --color=auto mysql

发现3306端口进程存在,我们再看下错误日志,看有没有报错

[root@centos-7 ~]# cat /data/mysql8.0.20/mysql3306/logs/mysql_error.log

日志内容太多我就不粘贴了,我查看后发现未报错,从以上内容我们可以认为3306端口实例已经正常启动了。

11.2启动3307端口实例

[root@centos-7 ~]# /usr/local/mysql8.0.20/bin/mysqld_safe --defaults-file=/data/mysql8.0.20/mysql3307/tmp/mysql3307.cnf --user=mysql &

[2] 8458

[root@centos-7 ~]# 2020-07-13T06:33:52.877251Z mysqld_safe Logging to '/data/mysql8.0.20/mysql3307/logs/mysql_error.log'.

2020-07-13T06:33:52.937512Z mysqld_safe Starting mysqld daemon with databases from /data/mysql8.0.20/mysql3307/data

我们查看下后台进程是否有3307端口这个实例

[root@centos-7 ~]# ps aux | grep mysql

root      6649  0.0  0.0 113620  1720 pts/0    S    14:26   0:00 /bin/sh /usr/local/mysql8.0.20/bin/mysqld_safe --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --user=mysql

mysql     7024  1.3 11.4 3092876 443472 pts/0  Sl   14:26   0:09 /usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --basedir=/usr/local/mysql8.0.20 --datadir=/data/mysql8.0.20/mysql3306/data --plugin-dir=/usr/local/mysql8.0.20/lib/plugin --user=mysql --log-error=/data/mysql8.0.20/mysql3306/logs/mysql_error.log --pid-file=centos-7.shared.pid --socket=/tmp/mysql3306.sock --port=3306

root      8458  0.0  0.0 113620  1720 pts/0    S    14:33   0:00 /bin/sh /usr/local/mysql8.0.20/bin/mysqld_safe --defaults-file=/data/mysql8.0.20/mysql3307/tmp/mysql3307.cnf --user=mysql

mysql     8830  1.5 11.2 2961232 435696 pts/0  Sl   14:33   0:04 /usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3307/tmp/mysql3307.cnf --basedir=/usr/local/mysql8.0.20 --datadir=/data/mysql8.0.20/mysql3307/data --plugin-dir=/usr/local/mysql8.0.20/lib/plugin --user=mysql --log-error=/data/mysql8.0.20/mysql3307/logs/mysql_error.log --pid-file=centos-7.shared.pid --socket=/data/mysql8.0.20/mysql3307/tmp/mysql3307.sock --port=3307

root      9861  0.0  0.0 112824   980 pts/0    S+   14:39   0:00 grep --color=auto mysql

我们看到后台进程有3307这个实例,同时也看到了3306端口实例。

我们再看下3307端口实例启动过程中是否有报错

[root@centos-7 ~]# cat /data/mysql8.0.20/mysql3307/logs/mysql_error.log

日志太多不粘贴了,我查看后发现未报错,从以上内容我们可以认为3307端口实例也已经正常启动了。

12.登录数据库

12.1登录3306端口实例

[root@centos-7 ~]# /usr/local/mysql8.0.20/bin/mysql -uroot -p -S /tmp/mysql3306.sock

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.20 MySQL Community Server - GPL

 

Copyright (c) 2000, 2020, 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.

 

mysql> show variables like 'port';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port          | 3306  |

+---------------+-------+

1 row in set (0.00 sec)

3306端口实例登录成功

12.2登录3307端口实例

/usr/local/mysql8.0.20/bin/mysql -uroot -p -S /data/mysql8.0.20/mysql3307/tmp/mysql3307.sock

[root@centos-7 ~]# /usr/local/mysql8.0.20/bin/mysql -uroot -p -S /data/mysql8.0.20/mysql3307/tmp/mysql3307.sock

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.20 MySQL Community Server - GPL

 

Copyright (c) 2000, 2020, 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.

 

mysql> show variables like 'port';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port          | 3307  |

+---------------+-------+

1 row in set (0.01 sec)

3307端口实例也登录成功了

13.修改密码

13.1修改3306端口实例密码

mysql>alter user root@localhost identified by 'Root!3306'

13.1修改3307端口实例密码

mysql>alter user root@localhost identified by 'Root!3307'


实战成功完成。