最近由于工作的需要,需要在同一台服务器上搭建两个RedisMySQL的实例。
多实例:就是在一台机器上面开启多个不同的端口(如Redis6379/6380MySQL3306/3307等),运行多个服务进程;公用一套安装程序,使用不同的配置文件,数据文件。

1. Redis多实例配置

1.1 查看主机Redis信息

  1. ps命令查看Redis进程
1
2
3
[root@localhost ~] ps -ef |grep redis
root 1706 1 0 2019 ? 04:12:09 /usr/local/bin/redis-server *:6379
root 18174 2560 0 15:35 pts/0 00:00:00 grep redis
  1. 查找配置文件位置
1
2
[root@localhost ~] locate redis.conf
/etc/redis.conf

1.2 拷贝配置文件并修改

  1. 拷贝redis.conf并命名为redis6380.conf,并修改参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~] cp /etc/redis.conf /etc/redis6380.conf
[root@localhost ~] vim /etc/redis6380.conf
# 查找 /pidfile 找到pid位置
# pidfile /var/run/redis.pid  #修改pid,每个实例需要运行在不同的pid
pidfile /var/run/redis6380.pid
#
# 查找 /port 6379 找到端口位置
# port 6379 #修改端口
port 6380
#                      
# 查找 /dir 找到数据目录位置
# dir /mnt/newdatadrive/data/redis #修改数据存放目录
dir /mnt/newdatadrive/data/redis6380
#
# 已开启Redis持久化
appendonly yes
  1. 准备上面配置的文件
1
2
[root@localhost ~] mkdir –p /mnt/newdatadrive/data/redis6380
[root@localhost ~] cp /var/run/redis.pid /var/run/redis6380.pid

1.3 启动测试

  1. 启动6380端口Redis服务,并查看Redis进程
1
2
3
4
5
[root@localhost ~] /usr/local/bin/redis-server /etc/redis6380.conf
[root@localhost ~] ps -ef |grep redis
root 1706 1 0 2019 ? 04:12:00 /usr/local/bin/redis-server *:6379
root 15967 1 0 12:16 ? 00:00:00 /usr/local/bin/redis-server *:6380
root 15994 8014 0 12:16 pts/2 00:00:00 grep redis
  1. 测试登录Redis客户端
1
2
[root@localhost ~] redis-cli -p 6380
127.0.0.1:6380> QUIT #退出
  1. 停止6380端口的Redis服务
1
redis-cli -p 6380 shutdown

1.4 Redis数据迁移

  1. 登录原Redis客户端(6379)
1
2
3
4
5
6
[root@localhost ~] redis-cli -p 6379
127.0.0.1:6379> SAVE #数据备份
127.0.0.1:6379> CONFIG GET dir #查看Redis数据目录
1) "dir"
2) "/mnt/newdatadrive/data/redis"
127.0.0.1:6379> QUIT #退出
  1. 拷贝数据文件appendonly.aofdump.rdb6380
1
2
3
4
5
6
7
8
# 查看6379的数据文件
[root@localhost ~] cd /mnt/newdatadrive/data/redis && ll
total 55176
-rw-r--r-- 1 root root 55411226 Feb 11 09:25 appendonly.aof
-rw-r--r-- 1 root root 1017181 Feb 11 12:28 dump.rdb
# 拷贝到6380
[root@localhost ~] \cp /mnt/newdatadrive/data/redis/appendonly.aof /mnt/newdatadrive/data/redis6380/appendonly.aof
[root@localhost ~] \cp /mnt/newdatadrive/data/redis/dump.rdb /mnt/newdatadrive/data/redis6380/dump.rdb
  1. 启动6380端口Redis服务,导入AOF数据文件
1
2
[root@localhost ~] /usr/local/bin/redis-server /etc/redis6380.conf
[root@localhost ~] redis-cli -p 6380 --pipe < /mnt/newdatadrive/data/redis6380/appendonly.aof
  1. 登录Redis查看数据
1
2
[root@localhost ~] redis-cli -p 6380
127.0.0.1:6380> #输入具体命令查看数据

1.5 配置远程可访问

  1. 修改配置文件redis6380.conf
1
2
3
4
5
6
7
[root@localhost ~] vim /etc/redis6380.conf
# 查找 /bind 找到:bind 127.0.0.1并注释,其它ip地址也可访问
# bind 127.0.0.1
#
# 查找 /requirepass 去掉注释#,并把foobared 替换为密码,例如:password123456
# requirepass foobared
requirepass password123456
  1. 开启防火墙的端口号规则(安全组),将6380端口号开通
1
[root@localhost ~] /sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT
  1. 修改完成后,要在服务里重启Redis服务才能使设置生效
1
/usr/local/bin/redis-server /etc/redis6380.conf
  1. 测试远程访问
1
2
C:\Users\zc> redis-cli -h 192.168.111.226 -p 6380 -a password123456
192.168.111.226:6380>
  1. 停止6380Redis服务也需要密码
1
[root@localhost ~] redis-cli -p 6380 -a password123456 shutdown

2. MySQL多实例配置

2.1 查看主机MySQL信息

  1. 查看现有MySQL数据库实例占用端口
1
2
3
[root@localhost ~] netstat -anp | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 1089/mysqld
unix 2 [ ACC ] STREAM LISTENING 20497 1089/mysqld /var/lib/mysql/mysql.sock

须先关闭单实例,跟多实例会有冲突

  • 备份数据:[root@localhost ~] mysqldump -P 3306 -u root -p --all-databases > /home/backup/data3306.bak
  • 停止单实例服务:[root@localhost ~] service mysqld stop
  1. 查找配置文件位置
1
2
3
[root@localhost ~] locate my.cnf
/etc/my.cnf
/etc/my.cnf.d

2.2 添加一个3307端口的实例

  1. 拷贝my.cnf并命名为my3307.cnf,并修改参数,主要修改port,sockt,datadir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~] cp /etc/my.cnf /etc/my3307.cnf
[root@localhost ~] vi /etc/my3307.cnf
[mysqld]
# server端字符集
character-set-server=utf8
collation-server=utf8_general_ci
user=root
# 修改端口
port=3307
# 修改数据存放目录
datadir=/var/lib/mysql3307
# 客户端连接socket
socket=/var/lib/mysql/mysql3307.sock
# 修改日志文件
log-error=/var/log/mysqld3307.log
# 修改pid,每个实例需要运行在不同的pid
pid-file=/var/run/mysqld/mysqld3307.pid
# 解决问题:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true
# skip_grant_tables
[mysql]
socket=/var/lib/mysql/mysql3307.sock
default-character-set=utf8
[mysql.server]
default-character-set=utf8
[mysql_safe]
default-character-set=utf8
[client]
socket=/var/lib/mysql/mysql3307.sock
default-character-set=utf8
  1. 初始化数据库
1
2
3
# 写入host避免反解析报错
[root@localhost ~] echo "127.0.0.1 `hostname`" >> /etc/hosts && cat /etc/hosts
[root@localhost ~] mysqld --defaults-file=/etc/my3307.cnf --initialize-insecure
  1. 启动3307端口MySQL服务,并查看MySQL进程
1
[root@localhost ~] mysqld --defaults-file=/etc/my3307.cnf --user=root &
  1. 登录MySQL
1
2
3
4
# 多实例为root增加密码
[root@localhost ~] mysqladmin -u root -S /var/lib/mysql/mysql3307.sock password '123qwe'
# 登录
[root@localhost ~] mysql -S /var/lib/mysql/mysql3307.sock -p
  1. 停止本实例MySQL服务
1
[root@localhost ~] mysqladmin -u root -S /var/lib/mysql/mysql3307.sock shutdown

2.3 再添加一个3308端口的实例

  1. 拷贝my.cnf并命名为my3308.cnf,并修改参数,主要修改port,sockt,datadir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~] cp /etc/my.cnf /etc/my3308.cnf
[root@localhost ~] vi /etc/my3308.cnf
[mysqld]
# server端字符集
character-set-server=utf8
collation-server=utf8_general_ci
user=root
# 修改端口
port=3308
# 修改数据存放目录
datadir=/var/lib/mysql3308
# 客户端连接socket
socket=/var/lib/mysql/mysql3308.sock
# 修改日志文件
log-error=/var/log/mysqld3308.log
# 修改pid,每个实例需要运行在不同的pid
pid-file=/var/run/mysqld/mysqld3308.pid
# 解决问题:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true
# skip_grant_tables
[mysql]
socket=/var/lib/mysql/mysql3308.sock
default-character-set=utf8
[mysql.server]
default-character-set=utf8
[mysql_safe]
default-character-set=utf8
[client]
socket=/var/lib/mysql/mysql3308.sock
default-character-set=utf8
  1. 初始化数据库
1
[root@localhost ~] mysqld --defaults-file=/etc/my3308.cnf --initialize-insecure
  1. 启动3308端口MySQL服务
1
[root@localhost ~] mysqld --defaults-file=/etc/my3308.cnf --user=root &
  1. 登录MySQL
1
2
3
4
# 多实例为root增加密码
[root@localhost ~] mysqladmin -u root -S /var/lib/mysql/mysql3308.sock password '123qwe'
# 登录
[root@localhost ~] mysql -S /var/lib/mysql/mysql3308.sock -p
  1. 停止本实例MySQL服务
1
[root@localhost ~] mysqladmin -u root -S /var/lib/mysql/mysql3308.sock shutdown

2.4 实例3307开启远程访问

  1. 开启3307端口防火墙
1
[root@localhost ~] /sbin/iptables -I INPUT -p tcp --dport 3307 -j ACCEPT
  1. 测试远程访问
1
2
C:\Users\zc>mysql -h 192.168.111.227 -P 3307 -u root -p
Enter password: ******