单主单从
name | ip | |
---|---|---|
node1 | 192.168.44.100 | 安装mycat |
node5 | 192.168.44.33 | 主 |
node3 | 192.168.44.11 | 从 |
读写分离的前提是首先要先将数据库搭建成主从模式!
实现目标
实现在两台数据库主机上安装数据库实现主从并在另一台主机上搭建mycat组件实现读写分离
搭建主从
1.安装并初始化操作
在两台主机上安装数据库并初始化
[root@node3 ~]# yum install -y mariadb mariadb-server
[root@node5 ~]# yum install -y mariadb mariadb-server
初始化数据库
#####两台主机同样操作
[root@node5 ~]# systemctl start mariadb ####首先要开启才能初始化
[root@node5 ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): #默认按回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password: #输入数据库root密码000000
Re-enter new password: #重复输入密码000000
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n ###在这个要n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
2.修改配置文件
主从的配置文件 /etc/my.cnf
###主节点###
[root@node5 ~]# vim /etc/my.cnf
[mysqld]
server_id=33 #id随意两台id不同即可
log_bin=mysql-bin
###从节点##
[root@node3 ~]# vi /etc/my.cnf
[mysqld]
server_id=11
log_bin=mysql-bin
重启所有节点数据库systemctl restart mariadb
一:配置主从
1:添加用来链接的用户
在主与从的链接过程中需要有一个用户进行链接,这个用户也可以是root用户不过使用root链接不安全
[root@node5 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
........
MariaDB [(none)]> create user user001;
Query OK, 0 rows affected (0.018 sec)
2:给链接用户授权
为了试验这里赋予所有权限,生产环境不适合
MariaDB [(none)]> grant all privileges on *.* to 'user001'@'%' identified by '000000' ;
Query OK, 0 rows affected (0.013 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user001'@'%' identified by '000000' ;
Query OK, 0 rows affected (0.003 sec)
3:从链接主
首先在主上查看bin-log文件和接入点
MariaDB [(none)]> show master status ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 2299 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
到从节点改变“主”人
MariaDB [(none)]> change master to master_user='user001',master_host='192.168.44.33',master_password='000000',master_log_file='mysql-bin.000004',master_log_pos2209;
Query OK, 0 rows affected (0.003 sec)
在从节点中开启从属的功能
MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.000 sec)
查看状态
MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.000 sec)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.44.33
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 2299
Relay_Log_File: node3-relay-bin.000007
Relay_Log_Pos: 2413
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes ##此处两处都为yes才行
Slave_SQL_Running: Yes
Replicate_Do_DB:
可能用到的命令:
slave的命令:
RESET SLAVE
SHOW SLAVE HOSTS
SHOW SLAVE STATUS
START SLAVE
STOP SLAVE
master的命令:
CHANGE MASTER TO
PURGE BINARY LOGS
RESET MASTER
SHOW BINARY LOGS
SHOW MASTER STATUS
4:验证
在主节点上创建一个表然后看从节点上有没有出现
此处不在演示
二:配置mycat插件
实现读写分离的关键就是将mycat插件与主从相连然后实现读写分离
MyCat 是目前最流行的基于 java 语言编写的数据库中间件,是一个实现了 MySQL 协议 的服务器,前端用户可以把它看作是一个数据库代理,用 MySQL 客户端工具和命令行访问,而其后端可以用 MySQL 原生协议与多个 MySQL 服务器通信,也可以用 JDBC 协议与大多数主流数据库服务器通信,其核心功能是分库分表。配合数据库的主从模式还可实现读写分离。
1.安装mycat
mycat是由Java写的所以在安装启动前需要安装一下Java依赖
[root@mall ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
...
[root@mall ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
安装mycat的方式有很多种这里采用上传安装包的方式安装
解压到/usr/local下面并授权
[root@node1 ~]# tar -xzvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@node1 ~]# chmod -R 777 /usr/local/mycat/
2.修改schema.xml文件
schema.xml文件在mycat目录下的conf(/usr/local/mycat/conf)目录下
schema.xml文件是来决定呢些主机进行读写分离和逻辑数据表的
删除多余部分仅留这部分即可
[root@node1 conf]# vim schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="<