MySQL多实例
1.实例
1.单实例:
1个进程 + 多个线程 + 单个预分配的内存空间
2.多实例:
多个进程 + 多个线程 + 多个预分配的内存空间
3.MySQL多实例:
1)多个端口
2)多个socket文件
3)多个server_id
4)多个日志文件
5)多个配置文件
6)多个数据目录
2.创建多实例存放目录
[root@db02 /service]# mkdir /service/{3307,3308,3309}
3.配置多个配置文件
[root@db02 /service]# vim /service/3307/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3307/data
port=3307
socket=/service/3307/data/mysql.sock
server_id=7
log-error=/service/3307/data/mysql.err
[root@db02 /service]# vim /service/3308/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3308/data
port=3308
socket=/service/3308/data/mysql.sock
server_id=8
log-error=/service/3308/data/mysql.err
[root@db02 /service]# vim /service/3309/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3309/data
port=3309
socket=/service/3309/data/mysql.sock
server_id=9
log-error=/service/3309/data/mysql.err
4.初始化多实例数据目录
[root@db02 /service]# cd mysql/scripts/
[root@db02 /service/mysql/scripts]# ./mysql_install_db --defaults-file=/service/3307/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3307/data
[root@db02 /service/mysql/scripts]# ./mysql_install_db --defaults-file=/service/3308/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3308/data
[root@db02 /service/mysql/scripts]# ./mysql_install_db --defaults-file=/service/3309/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3309/data
[root@db02 /service]# tree -L 3 ./
./
├── 3307
│ ├── data
│ │ ├── ibdata1
│ │ ├── ib_logfile0
│ │ ├── ib_logfile1
│ │ ├── mysql
│ │ ├── mysql.err
│ │ ├── performance_schema
│ │ └── test
│ └── my.cnf
├── 3308
│ ├── data
│ │ ├── ibdata1
│ │ ├── ib_logfile0
│ │ ├── ib_logfile1
│ │ ├── mysql
│ │ ├── mysql.err
│ │ ├── performance_schema
│ │ └── test
│ └── my.cnf
├── 3309
│ ├── data
│ │ ├── ibdata1
│ │ ├── ib_logfile0
│ │ ├── ib_logfile1
│ │ ├── mysql
│ │ ├── mysql.err
│ │ ├── performance_schema
│ │ └── test
│ └── my.cnf
├── mysql -> /service/mysql-5.6.42
└── mysql-5.6.42
5.授权
[root@db02 /service]# chown -R mysql.mysql /service/
6.启动多实例
[root@db02 /service]# mysqld_safe --defaults-file=/service/3307/my.cnf &
[root@db02 /service]# mysqld_safe --defaults-file=/service/3308/my.cnf &
[root@db02 /service]# mysqld_safe --defaults-file=/service/3309/my.cnf &
7.验证启动
[root@db02 /service]# netstat -lntp | grep 330
tcp6 0 0 :::3306 :::* LISTEN 24992/mysqld
tcp6 0 0 :::3307 :::* LISTEN 32069/mysqld
tcp6 0 0 :::3308 :::* LISTEN 32234/mysqld
tcp6 0 0 :::3309 :::* LISTEN 32394/mysqld
8.登录验证多实例
[root@db02 /service]# mysql -uroot -S /service/3307/data/mysql.sock -e 'show variables like "server_id"'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 7 |
+---------------+-------+
[root@db02 /service]# mysql -uroot -S /service/3308/data/mysql.sock -e 'show variables like "server_id"'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 8 |
+---------------+-------+
[root@db02 /service]# mysql -uroot -S /service/3309/data/mysql.sock -e 'show variables like "server_id"'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 9 |
+---------------+-------+
9.多实例设置密码
[root@db02 /service]# mysqladmin -uroot password -S /service/3307/data/mysql.sock
New password: 123
Confirm new password: 123
[root@db02 /service]# mysqladmin -uroot password -S /service/3308/data/mysql.sock
New password: 123
Confirm new password: 123
[root@db02 /service]# mysqladmin -uroot password -S /service/3309/data/mysql.sock
New password: 123
Confirm new password: 123
10.配置简单连接数据库多实例
#1.编写连接数据库文件
[root@db02 /service]# cat /usr/bin/mysql3307
mysql -uroot -p123 -S /service/3307/data/mysql.sock
[root@db02 /service]# cat /usr/bin/mysql3308
mysql -uroot -p123 -S /service/3308/data/mysql.sock
[root@db02 /service]# cat /usr/bin/mysql3309
mysql -uroot -p123 -S /service/3309/data/mysql.sock
#2.授权执行权限
[root@db02 /service]# chmod +x /usr/bin/mysql*
#3.直接使用命令连接
[root@db02 /service]# mysql3307
[root@db02 /service]# mysql3308
[root@db02 /service]# mysql3309
数据库多实例主从
0)配置主库的binlog
[root@db02 /service]# vim /service/3307/my.cnf
... ...
log_bin=/service/3307/data/mysql-bin
1.主库授权主从的用户
mysql> grant replication slave on *.* to rep@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
2.主库查看binlog信息
mysql> show master status;
+------------------+----------+
| File | Position |
+------------------+----------+
| mysql-bin.000001 | 120 |
+------------------+----------+
1 row in set (0.01 sec)
3.从库配置同步主库
#1.从库需要知道主库的信息
1)主库的地址
2)主库的端口
3)主库授权的用户
4)主库授权的用户的密码
5)主库的binlog日志名字
6)binlog的位置点
#2.从库配置主从同步
mysql> change master to
-> master_host='172.16.1.52',
-> master_port=3307,
-> master_user='rep',
-> master_password='123',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
change master to
master_host='172.16.1.52',
master_port=3309,
master_user='rep',
master_password='123',
master_log_file='mysql-bin.000001',
master_log_pos=120;
4.启动主从同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
5.查看主从状态
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: rep
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 416
Relay_Log_File: db02-relay-bin.000002
Relay_Log_Pos: 579
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
6.主从同步问题
1)IO线程为NO
1.检查网络
[root@db02 ~]# ping 127.0.0.1
2.检查端口
[root@db02 ~]# telnet 127.0.0.1 3307
3.查看防火墙
4.主从同步的用户和密码错误
检查change master to语句信息是否正确
5.数据库反向解析
2)SQL线程为NO
1.主库有的数据,从库没有
2.从库有的数据,主库没有
3.主库与从库数据不一致
手动停掉从库 stop slave;
手动同步
change master to
master_host='172.16.1.52',
master_user='rep',
master_password='123',
master_port=3309,
master_log_file='mysql-bin.000001',
master_log_pos=201
启动 start slave
查看