MySql单台服务器运行多实例

基础环境

华为云服务器CentOS8.0、MySql5.7.40

操作步骤

1. 要确定你的机器上面已经安装MySQL,我的MySQL在/usr/loacl/ 下面:

[root@hecs-141427 ~]# cd /usr/local
[root@hecs-141427 local]# ls
bin  etc  games  hostguard  include  jdk  lib  lib64  libexec  maven  mysql  nacos  redis  redis-6.0.8  redis-6.0.8.tar.gz  sbin  share  src  uniagent
[root@hecs-141427 local]# cd mysql
[root@hecs-141427 mysql]# ls
bin  data  docs  include  lib  LICENSE  man  README  share  support-files
[root@hecs-141427 mysql]# cd data
[root@hecs-141427 data]# ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql             mysql-bin.000002  mysql-bin.index     private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  mysql-bin.000003  performance_schema  public_key.pem   server-key.pem
[root@hecs-141427 data]# 

 2.进入/usr/loacl/mysql文件夹,在data下分别创建文件夹,如 : 3307data  3308data :

[root@hecs-141427 mysql]# ls
bin  data  docs  include  lib  LICENSE  man  README  share  support-files
[root@hecs-141427 mysql]# mkdir -p 3307data
[root@hecs-141427 mysql]# mkdir -p 3308data
[root@hecs-141427 mysql]# ls
3307data  3308data  bin  data  docs  include  lib  LICENSE  man  README  share  support-files
[root@hecs-141427 mysql]# 

 3.在/usr/loacl/mysql/bin/下初始化数据库,指定到新建文件夹:

[root@hecs-141427 mysql]# cd bin
[root@hecs-141427 bin]# ls
innochecksum    myisampack         mysqlcheck                  mysqld-debug   mysql_embedded    mysql_secure_installation  mysql_tzinfo_to_sql  resolveip
lz4_decompress  my_print_defaults  mysql_client_test_embedded  mysqld_multi   mysqlimport       mysqlshow                  mysql_upgrade        resolve_stack_dump
myisamchk       mysql              mysql_config                mysqld_safe    mysql_install_db  mysqlslap                  mysqlxtest           zlib_decompress
myisam_ftdump   mysqladmin         mysql_config_editor         mysqldump      mysql_plugin      mysql_ssl_rsa_setup        perror
myisamlog       mysqlbinlog        mysqld                      mysqldumpslow  mysqlpump         mysqltest_embedded         replace
[root@hecs-141427 bin]# 
[root@hecs-141427 bin]# ./mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/3307data --user=mysql
[root@hecs-141427 bin]# ./mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/3308data --user=mysql
[root@hecs-141427 bin]# 

 其中initialize-insecure表示不生成mysql数据库root用户随机密码,即root密码为空。

4.在/etc下分别创建名为my3307.cnf和my3308.cnf的文件,并插入配置内容:

[root@hecs-141427 etc]# vim my3307.cnf
[root@hecs-141427 etc]# cat my3307.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/3307data
port = 3307
socket=/tmp/mysql3307.sock
 
symbolic-links=0
log-error=/var/log/mysqld3307.log
pid-file=/tmp/mysqld/mysqld3307.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
log-bin=mysql-bin 
binlog-format=ROW 
server_id=2 
max_connections=1000
 
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
[root@hecs-141427 etc]# 
[root@hecs-141427 etc]# vim my3308.cnf
[root@hecs-141427 etc]# cat my3308.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/3308data
port = 3308
socket=/tmp/mysql3308.sock
 
symbolic-links=0
log-error=/var/log/mysqld3308.log
pid-file=/tmp/mysqld/mysqld3308.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
log-bin=mysql-bin 
binlog-format=ROW 
server_id=3 
max_connections=1000
 
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
[root@hecs-141427 etc]# 

5.启动测试:

[root@hecs-141427 bin]# 
[root@hecs-141427 bin]# ./mysql_safe --defaults-file=/etc/my3307.cnf &
[1] 293049
[root@hecs-141427 bin]# -bash: ./mysql_safe: No such file or directory
--user=mysql^C
[1]+  Exit 127                ./mysql_safe --defaults-file=/etc/my3307.cnf
[root@hecs-141427 bin]# ./mysql_safe --defaults-file=/etc/my3307.cnf --user=mysql &
[1] 293126
[root@hecs-141427 bin]# -bash: ./mysql_safe: No such file or directory
^C
[1]+  Exit 127                ./mysql_safe --defaults-file=/etc/my3307.cnf --user=mysql
[root@hecs-141427 bin]# ./mysqld_safe --defaults-file=/etc/my3307.cnf --user=mysql &
[1] 293193
[root@hecs-141427 bin]# 2022-10-22T16:04:45.325460Z mysqld_safe Logging to '/var/log/mysqld3307.log'.
2022-10-22T16:04:45.350539Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/3307data
[root@hecs-141427 bin]# ./mysqld_safe --defaults-file=/etc/my3308.cnf --user=mysql &
[2] 293619
[root@hecs-141427 bin]# 2022-10-22T16:06:22.724304Z mysqld_safe Logging to '/var/log/mysqld3308.log'.
2022-10-22T16:06:22.749201Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/3308data

 其中 --defaults-file是指定配置文件,&表示后台启动。

 是否启动成功,可以通过查看mysql进程 :

[root@hecs-141427 etc]# ps -ef | grep mysql
root      277439       1  0 Oct22 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/tmp/mysqld/mysqld.pid
mysql     277806  277439  0 Oct22 ?        00:00:17 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/tmp/mysqld/mysqld.pid --socket=/tmp/mysql.sock --port=3306
root      293193  292541  0 00:04 pts/0    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/etc/my3307.cnf --user=mysql
mysql     293545  293193  0 00:04 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/3307data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld3307.log --pid-file=/tmp/mysqld/mysqld3307.pid --socket=/tmp/mysql3307.sock --port=3307
root      293619  292541  0 00:06 pts/0    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/etc/my3308.cnf --user=mysql
mysql     293971  293619  0 00:06 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my3308.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/3308data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld3308.log --pid-file=/tmp/mysqld/mysqld3308.pid --socket=/tmp/mysql3308.sock --port=3308
root      294018  292891  0 00:07 pts/1    00:00:00 grep --color=auto mysql
[root@hecs-141427 etc]# 

 6.登陆:

注意在/usr/loacl/mysql-5.7.18/bin下执行:

[root@hecs-141427 bin]# ./mysql -uroot -p -P3307
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 741
Server version: 5.7.40-log MySQL Community Server (GPL)

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

 注意:因为未设置密码,此时密码随机。

7.修改密码:

mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> 

 用户界面远程登陆需要配置:

mysql> grant all privileges on *.* to root@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

*.* : 其中第一个*表示所有数据库名,第二个*表示所有的数据库表

root@'%':root表示用户名,%表示ip地址,可以具体到某个ip 如:[email protected]

然后执行权限刷新:

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

3308端口同样按照6~8步骤进行设置即可。

因为做了软链接,可以如下登录:

[root@hecs-141427 ~]# mysql -uroot -P3307 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.40-log MySQL Community Server (GPL)

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

8. 开放3307和3308端口

[root@hecs-141427 ~]# firewall-cmd --list-ports
3306/tcp 6379/tcp 6380/tcp 6381/tcp 8848/tcp 26379/tcp 26380/tcp 26381/tcp
[root@hecs-141427 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2022-10-20 14:12:54 CST; 2 days ago
     Docs: man:firewalld(1)
 Main PID: 211296 (firewalld)
    Tasks: 2 (limit: 23712)
   Memory: 26.2M
   CGroup: /system.slice/firewalld.service
           └─211296 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

Oct 20 14:12:53 hecs-141427 systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 20 14:12:54 hecs-141427 systemd[1]: Started firewalld - dynamic firewall daemon.
Oct 20 14:12:54 hecs-141427 firewalld[211296]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please c>

[root@hecs-141427 ~]# firewall-cmd --zone=public --add-port=3307/tcp --permanent
success
[root@hecs-141427 ~]# firewall-cmd --zone=public --add-port=3308/tcp --permanent
success
[root@hecs-141427 ~]# systemctl restart firewalld.service
[root@hecs-141427 ~]# firewall-cmd --list-ports
3306/tcp 3307/tcp 3308/tcp 6379/tcp 6380/tcp 6381/tcp 8848/tcp 26379/tcp 26380/tcp 26381/tcp
[root@hecs-141427 ~]# 

猜你喜欢

转载自blog.csdn.net/Genius_zhu/article/details/127469523