Jul 11, 2019
mysql主从复制及读写分离的尝试
centos7 rpm安装mysql
此次使用的是MySQL Community Server 5.5.62
可以自己选择版本下载页面
附上本次下载地址
mysql server
mysql client
安装mysql-server
1
rpm -ivh MySQL-server-5.5.62-1.el7.x86_64.rpm
注:
-i
安装
-v
可视化-h
显示进度
出现一下错误
1
2
3
4
5
error: Failed dependencies:
libaio.so.1()(64bit) is needed by MySQL-server-5.5.62-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by MySQL-server-5.5.62-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by MySQL-server-5.5.62-1.el7.x86_64
perl(Data::Dumper) is needed by MySQL-server-5.5.62-1.el7.x86_64
提示缺少以下依赖
- libaio
- perl(Data::Dumper)
下载地址
libaio-0.3.109-13.el7.x86_64.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
将以上依赖安装后再次安装mysql-server
又出现以下错误
1
2
3
Preparing... ################################# [100%]
file /usr/share/mysql/charsets/README from install of MySQL-server-5.5.62-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64
file /usr/share/mysql/charsets/Index.xml from install of MySQL-server-5.5.62-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64
先卸载旧的mysql-libs
1
yum remove mysql-libs
再次安装mysql-server
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
warning: MySQL-server-5.5.62-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:MySQL-server-5.5.62-1.el7 ################################# [100%]
warning: user mysql does not exist - using root
warning: group mysql does not exist - using root
190711 9:20:16 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
190711 9:20:16 [Note] /usr/sbin/mysqld (mysqld 5.5.62) starting as process 12846 ...
190711 9:20:16 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
190711 9:20:16 [Note] /usr/sbin/mysqld (mysqld 5.5.62) starting as process 12853 ...
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h iZm5e2cvrl3la2bzgnuxnnZ password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
Please report any problems at http://bugs.mysql.com/
有以上提示就安装成功了
安装mysql-client
1
rpm -ivh MySQL-client-5.5.62-1.el7.x86_64.rpm
根据先前mysql-server安装成功后的提示依次执行
1
2
3/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h iZm5e2cvrl3la2bzgnuxnnZ password 'new-password'
/usr/bin/mysql_secure_installation
以交互式方式
- 设置root密码
- 移除test库
- 禁止root远程登录
- ...
配置master数据库
my.cnf文件新增
1
2
3
4server-id = 1
log-bin=/var/lib/mysql/mysql-bin
binlog-ignore-db = mysql
binlog-ignore-db = information_schema重启master数据库
查看master状态
1
2
3
4
5
6
7mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000001 | 339 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)给slave数据库分配账号
1
grant replication slave on *.* to 'slave'@'%' identified by 'password';
此处
slave@%
中的%
可以换成slave的ip
配置slave数据库
my.cnf文件新增
1
2
3server-id = 2
replicate-ignore-db = mysql
replicate-ignore-db = information_schema连接到master数据库
1
CHANGE MASTER TO MASTER_HOST='1.1.1.1(masterIp)',MASTER_USER='slave', MASTER_PASSWORD='password',MASTER_LOG_FILE=' mysql-binlog.000001',MASTER_LOG_POS=339;
填入master数据库地址
、先前从master数据库中查看到的binlog信息
,以及分配的账号密码
启用slave同步
1
start slave;
查看slave信息
1
show slave status\G;
显示如下即开启同步
1
2Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mycat读写分离配置
config/schema.xml
1 | <schema name="databasename" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> |
config/server.xml
1 | <user name="username" defaultAccount="true"> |