MySQL8.0.x多实例

1.1 准备
1.1.1 创建数据目录

mkdir -p /data/330{7..9}/data
chown -R mysql.mysql /data/*

结果:

[root@localhost ~]# mkdir -p /data/330{7..9}/data
[root@localhost ~]# ls -ld /data/*
drwxr-xr-x 3 root root 18 Nov 13 23:23 /data/3306
drwxr-xr-x 3 root root 18 Nov 21 21:42 /data/3307
drwxr-xr-x 3 root root 18 Nov 21 21:42 /data/3308
drwxr-xr-x 3 root root 18 Nov 21 21:42 /data/3309
[root@localhost ~]# chown -R mysql.mysql /data/*
[root@localhost ~]# ls -ld /data/*
drwxr-xr-x 3 mysql mysql 18 Nov 13 23:23 /data/3306
drwxr-xr-x 3 mysql mysql 18 Nov 21 21:42 /data/3307
drwxr-xr-x 3 mysql mysql 18 Nov 21 21:42 /data/3308
drwxr-xr-x 3 mysql mysql 18 Nov 21 21:42 /data/3309
[root@localhost ~]# 

1.1.2 配置文件
实例1:

cat > /etc/my3307.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3307/data
socket=/tmp/mysql3307.sock
server_id=7
port=3307
EOF

实例2:

cat > /etc/my3308.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3308/data
socket=/tmp/mysql3308.sock
server_id=8
port=3308
EOF

实例3:

cat > /etc/my3309.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3309/data
socket=/tmp/mysql3309.sock
server_id=9
port=3309
EOF

结果:

[root@localhost ~]# cat /etc/my3307.cnf  #实例1
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3307/data
socket=/tmp/mysql3307.sock
server_id=7
port=3307
[root@localhost ~]# cat /etc/my3308.cnf #实例2
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3308/data
socket=/tmp/mysql3308.sock
server_id=8
port=3308
[root@localhost ~]# cat /etc/my3309.cnf #实例3
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3309/data
socket=/tmp/mysql3309.sock
server_id=9
port=3309
[root@localhost ~]#

1.2 初始化数据

mysqld --defaults-file=/etc/my3307.cnf --initialize-insecure
mysqld --defaults-file=/etc/my3308.cnf --initialize-insecure
mysqld --defaults-file=/etc/my3309.cnf --initialize-insecure

结果:

[root@localhost ~]# mysqld --defaults-file=/etc/my3307.cnf --initialize-insecure
2021-11-21T13:45:09.343400Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.24-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.24) initializing of server in progress as process 9537
2021-11-21T13:45:09.365221Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-11-21T13:45:10.546944Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-11-21T13:45:12.129679Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost ~]# mysqld --defaults-file=/etc/my3308.cnf --initialize-insecure
2021-11-21T13:45:22.038192Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.24-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.24) initializing of server in progress as process 9580
2021-11-21T13:45:22.050197Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-11-21T13:45:23.067962Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-11-21T13:45:24.634758Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# mysqld --defaults-file=/etc/my3309.cnf --initialize-insecure
2021-11-21T13:45:29.217673Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.24-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.24) initializing of server in progress as process 9623
2021-11-21T13:45:29.237397Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-11-21T13:45:30.162730Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-11-21T13:45:31.525275Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost ~]# 

1.3 启动

mysqld_safe --defaults-file=/etc/my3307.cnf &
mysqld_safe --defaults-file=/etc/my3308.cnf &
mysqld_safe --defaults-file=/etc/my3309.cnf &

结果:

[root@localhost ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[1] 9666
[root@localhost ~]# Logging to '/data/3307/data/localhost.localdomain.err'.
2021-11-21T13:47:41.575789Z mysqld_safe Starting mysqld daemon with databases from /data/3307/data

[root@localhost ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &
[2] 9846
[root@localhost ~]# Logging to '/data/3308/data/localhost.localdomain.err'.
2021-11-21T13:47:46.860188Z mysqld_safe Starting mysqld daemon with databases from /data/3308/data

[root@localhost ~]# mysqld_safe --defaults-file=/etc/my3309.cnf &
[3] 10026
[root@localhost ~]# Logging to '/data/3309/data/localhost.localdomain.err'.
2021-11-21T13:47:54.233706Z mysqld_safe Starting mysqld daemon with databases from /data/3309/data

[root@localhost ~]# 

1.4 检查验证

netstat -lntup|grep 33

结果:

[root@localhost ~]# netstat -lntup|grep 33
tcp6       0      0 :::3306                 :::*                    LISTEN      9145/mysqld         
tcp6       0      0 :::3307                 :::*                    LISTEN      9805/mysqld         
tcp6       0      0 :::3308                 :::*                    LISTEN      9985/mysqld         
tcp6       0      0 :::3309                 :::*                    LISTEN      10165/mysqld        
tcp6       0      0 :::33060                :::*                    LISTEN      9145/mysqld

1.5 登录验证

mysql -S /tmp/mysql3307.sock
mysql -S /tmp/mysql3308.sock
mysql -S /tmp/mysql3309.sock

结果:

[root@localhost ~]# mysql -S /tmp/mysql3307.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> quit
Bye
[root@localhost ~]# mysql -S /tmp/mysql3308.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> quit
Bye
[root@localhost ~]# mysql -S /tmp/mysql3309.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> quit
Bye
[root@localhost ~]#

1.6 停止

mysqladmin -S /tmp/mysql3307.sock shutdown
mysqladmin -S /tmp/mysql3308.sock shutdown
mysqladmin -S /tmp/mysql3309.sock shutdown

结果:

[root@localhost ~]# mysqladmin -S /tmp/mysql3307.sock shutdown
2021-11-21T14:02:28.756113Z mysqld_safe mysqld from pid file /data/3307/data/localhost.localdomain.pid ended
[1]   Done                    mysqld_safe --defaults-file=/etc/my3307.cnf
[root@localhost ~]# mysqladmin -S /tmp/mysql3308.sock shutdown
2021-11-21T14:02:40.753111Z mysqld_safe mysqld from pid file /data/3308/data/localhost.localdomain.pid ended
[2]-  Done                    mysqld_safe --defaults-file=/etc/my3308.cnf
[root@localhost ~]# mysqladmin -S /tmp/mysql3309.sock shutdown
2021-11-21T14:02:43.923949Z mysqld_safe mysqld from pid file /data/3309/data/localhost.localdomain.pid ended
[3]+  Done                    mysqld_safe --defaults-file=/etc/my3309.cnf
[root@localhost ~]#

1.4 小结
1.停止数据库时候,可以登录到数据库,在交互模式下停止;(mysql> shutdown;)
2.这里初始化的时候使用的是 --initialize-insecure参数,所以不会生成密码,登录的时候也不需要输入密码。如果要生成密码,可以使用参数:--initialize,例如:

[root@localhost ~]# mysqld --defaults-file=/etc/my3309.cnf --initialize
2021-11-21T14:08:31.173035Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.24-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.24) initializing of server in progress as process 10258
2021-11-21T14:08:31.193484Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-11-21T14:08:31.734446Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-11-21T14:08:32.914291Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: (y8+1xr6W;#k   #初始化密码,登录的时候需要,登录后,需要修改密码才能做相关操作
[root@localhost ~]#
posted @ 2021-11-22 23:27  红桃Z  阅读(197)  评论(0编辑  收藏  举报