由于后续进行数据库读写分离,需要在开发环境中部署主从数据库,为了节省资源,主从数据库将部署在同一台服务器中,因此针对单机双实例场景配置进行说明。
此处并不对如何安装mysql进行说明,具体可见github脚本
实际上单机多实例,只需要对配置进行修改即可轻松实现:
- 创建主从数据库数据目录
1
2
3
4mkdir /usr/local/mysql/data
mkdir /usr/local/mysql/data_slave
chown -R mysql:mysql /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql/data_slave
配置my.cnf
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[mysqld_multi]
mysqld = /opt/mysql/server-5.6/bin/mysqld_safe
mysqladmin = /opt/mysql/server-5.6/bin/mysqladmin
log = /opt/mysql/server-5.6/mysqld_multi.log
[mysqld1]
socket = /tmp/mysqld_master.sock
port = 3306
pid-file = /opt/mysql/server-5.6/data/mysql_master.pid
datadir = /opt/mysql/server-5.6/data
character_set_server=utf8
log-bin=/opt/mysql/server-5.6/data/mysql-bin-master
server-id = 1
......
......
[mysqld2]
socket = /tmp/mysqld_slave.sock
port = 3307
pid-file = /opt/mysql/server-5.6/data_slave/mysql_slave.pid
datadir = /opt/mysql/server-5.6/data_slave
character_set_server=utf8
log-bin=/opt/mysql/server-5.6/data_slave/mysql-bin-slave
server-id = 2
......
......初始化数据库
1
2/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data_slave修改path
相关操作前需要将相应的mysql bin路径添加到path中1
2#临时添加到path中
export PATH=/usr/local/mysql/bin:$PATH启动/停止数据库
直接启动多实例:
1 | /usr/local/mysql/bin/mysqld_multi --defaults-file=/usr/local/mysql/my.cnf start |
指定实例启动
1 | /usr/local/mysql/bin/mysqld_multi --defaults-file=/usr/local/mysql/my.cnf start 1 |
直接停止多实例
1 | /usr/local/mysql/bin/mysqld_multi --defaults-file=/usr/local/mysql/my.cnf stop |
指定实例停止
1 | /usr/local/mysql/bin/mysqld_multi --defaults-file=/usr/local/mysql/my.cnf stop 1 |
登录到各自实例
1
2
3/usr/local/mysql/bin/mysql --socket=/tmp/mysqld_slave.sock
/usr/local/mysql/bin/mysql --socket=/tmp/mysqld_master.sock主从配置
1 | # 配置主库root和slave账号,在主库中操作 |
1 | # 导出主库数据,在主库中操作 |
1 | # 解锁,在主库中操作 |
1 | # 导入数据到从库,在从库中操作 |
1 | # 从库配置主库信息,在从库中操作,master_log_file和master_log_pos填写之前master的File和Position |