MariaDB主从复制和读写分离

一、基础环境

 

 

 

二、配置MariaDB服务

1.安装MariaDB

# yum install -y mariadb mariadb-server

# systemctl start mariadb

# systemctl enable mariadb

2.安全配置

# mysql_secure_installation

3.调优

# vi /etc/my.cnf.d/server.cnf

在[mysqld]插入以下项

[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON

4.授权访问

MariaDB [(none)]> use mysql;

MariaDB [mysql]> grant all privileges on *.* to 'root'@'ip' identified by 'your passwd';      //授权制定ip访问

MariaDB [mysql]> grant all privileges on *.* to 'root'@'%' identified by 'your passwd';       //授权所有ip访问

MariaDB [mysql]> flush privileges;

MariaDB [mysql]> \q

 

三、配置主从同步

1.配置node01主节点

# vim /etc/my.cnf.d/server.cnf

在 [mysqld] 追加以下项

[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON

server_id=1
log_bin=master-bin

更改配置后重启mariadb服务

# systemctl restart mariadb

创建用户tichy,slave节点通过用户tichy复制数据

MariaDB [mysql]> grant replication slave,replication client on *.* to 'tichy'@'10.100.1.%' identified by 'tichy';

MariaDB [mysql]> flush privileges;

查看主节点状态

MariaDB [(none)]> show master status;

2.配置node02和node03从节点

# vim /etc/my.cnf.d/server.cnf

[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON

server_id=2
relay_log=relay-log
read_only=ON

指向主库

MariaDB [(none)]> change master to master_host='10.100.1.121',master_user='tichy',master_password='tichy',master_log_file='master-bin.000001',master_log_pos=245;

node03和node02配置方法一样,但server_id一定要区分开

3.主从测试

在node01主节点上创建数据库、创建表、插入数据

创建库

MariaDB [(none)]> create database tichy;

创建表

MariaDB [(none)]> use tichy;
Database changed
MariaDB [tichy]> create table person(id int, name varchar(20), age int);
Query OK, 0 rows affected (0.01 sec)

插入数据

MariaDB [tichy]> insert into person values('1','tom','28'),('2','jerry','27');

查看库表

MariaDB [tichy]> select * from person;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | tom   |   28 |
|    2 | jerry |   27 |
+------+-------+------+
2 rows in set (0.00 sec)

在node02和node03从节点上查询

MariaDB [(none)]> select * from tichy.person;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | tom   |   28 |
|    2 | jerry |   27 |
+------+-------+------+
2 rows in set (0.00 sec)

可以看到库表数据,主从复制配置成功。

 

四、主从读写分离

 1.配置mysql-proxy

# wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/tichy/p/10974572.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值