创建多实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
实例:一个进程 + 多个线程 + 预分配的内存结构
多实例:多个进程 + 多个线程 + 多个预分配的内存结构

多个数据库实例不同之处:
配置文件:
1.端口
2.数据目录
3.socket文件
4.pid文件
5.错误日志

#查看默认配置
mysql> show variables like '%pid%';
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| pid_file | /app/mysql/data/db01.pid |
+---------------+--------------------------+
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#1.创建多个实例数据存放目录
[root@db01 ~]# mkdir /data/mysql/330{7,8,9} -p

#2.编写配置文件
[root@db01 ~]#vim /data/mysql/3307/my.cnf
[mysqld]
port=3307
basedir=/app/mysql
datadir=/data/mysql/3307/data
socket=/data/mysql/3307/data/mysql.sock
pid_file=/data/mysql/3307/data/mysql.pid
log_error=/data/mysql/3307/data/mysql.err

[root@db01 ~]# vim /data/mysql/3308/my.cnf
[mysqld]
port=3308
basedir=/app/mysql
datadir=/data/mysql/3308/data
socket=/data/mysql/3308/data/mysql.sock
pid_file=/data/mysql/3308/data/mysql.pid
log_error=/data/mysql/3308/data/mysql.err

[root@db01 ~]# vim /data/mysql/3309/my.cnf
[mysqld]
port=3309
basedir=/app/mysql
datadir=/data/mysql/3309/data
socket=/data/mysql/3309/data/mysql.sock
pid_file=/data/mysql/3309/data/mysql.pid
log_error=/data/mysql/3309/data/mysql.err

#3.初始化数据库
/app/mysql/scripts/mysql_install_db --user=mysql --basedir=/app/mysql --datadir=/data/mysql/3309/data

/app/mysql/scripts/mysql_install_db --user=mysql --basedir=/app/mysql --datadir=/data/mysql/3308/data

/app/mysql/scripts/mysql_install_db --user=mysql --basedir=/app/mysql --datadir=/data/mysql/3307/data

#4.启动数据库
[root@db01 ~]# mysqld_safe --defaults-file=/data/mysql/3307/my.cnf

[root@db01 ~]# mysqld_safe --defaults-file=/data/mysql/3308/my.cnf

[root@db01 ~]# mysqld_safe --defaults-file=/data/mysql/3309/my.cnf

#5.编写启动脚本
[root@db01 ~]# vim /usr/lib/systemd/system/mysql3307.service
[Unit]
Description=MySQL Server
After=network.target
Wants=multi-user.target

[Service]
ExecStart=/app/mysql/bin/mysqld_safe --defaults-file=/data/mysql/3307/my.cnf
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

[root@db01 ~]# vim /usr/lib/systemd/system/mysql3308.service
[Unit]
Description=MySQL Server
After=network.target
Wants=multi-user.target

[Service]
ExecStart=/app/mysql/bin/mysqld_safe --defaults-file=/data/mysql/3308/my.cnf
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

[root@db01 ~]# vim /usr/lib/systemd/system/mysql3309.service
[Unit]
Description=MySQL Server
After=network.target
Wants=multi-user.target

[Service]
ExecStart=/app/mysql/bin/mysqld_safe --defaults-file=/data/mysql/3309/my.cnf
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

#6.重新加载脚本
[root@db01 ~]# systemctl daemon-reload

#修改root密码
[root@db01 ~]# mysqladmin -uroot -S /data/mysql/3307/data/mysql.sock password '3307'
[root@db01 ~]# mysqladmin -uroot -S /data/mysql/3307/data/mysql.sock password '3308'
[root@db01 ~]# mysqladmin -uroot -S /data/mysql/3307/data/mysql.sock password '3309'

# 8.连接数据库
[root@db01 ~]# mysql -uroot -p3307 -S /data/mysql/3307/data/mysql.sock
[root@db01 ~]# mysql -uroot -p3308 -S /data/mysql/3308/data/mysql.sock
[root@db01 ~]# mysql -uroot -p3309 -S /data/mysql/3309/data/mysql.sock

# 9.简易连接方式(将链接命令写到bin下)
[root@db01 ~]# vim /usr/local/bin/mysql3307
mysql -uroot -p3307 -S /data/mysql/3307/data/mysql.sock

[root@db01 ~]# vim /usr/local/bin/mysql3308
mysql -uroot -p3308 -S /data/mysql/3308/data/mysql.sock

[root@db01 ~]# vim /usr/local/bin/mysql3309
mysql -uroot -p3309 -S /data/mysql/3309/data/mysql.sock

[root@db01 ~]# chmod +x /usr/local/bin/mysql330*


#连接时候只需要输入
mysql3307
mysql3308
mysql3309