mysql 主从复制搭建

osc_tzzfwr6n 2020-11-10 14:28:26
Mysql


mysql 主从复制搭建


-------------------------Mysql主从复制--------------------------------------
环境
master 192.168.100.150
slave01 192.168.100.160
slave02 192.168.100.170
amoeba 192.168.100.130
客户端mysql 192.168.100.120
--------------------------主服务器
master 192.168.100.150
1.修改/etc/my.cnf配置文件,增加服务器id,配置二进制日志选项
systemctl stop firewalld
set enforce 0
vim /etc/my.cnf
server-id = 11
log-bin=master-bin
log-slave-updates=true
systemctl restart mysqld.service
mysql -uroot -pabc123
mysql> grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123456';
mysql> flush privileges;
mysql> show master status;
--------------------------从服务器-----------------------
slave01 192.168.100.160
vim /etc/my.cnf
'//mysqld字段下面修改'
server-id = 22
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
mysql> change master to master_host='192.168.100.150',master_user='myslave',master_password='123456',master_log_file='master-bin.000013',master_log_pos=154;
flush privileges;
flush privileges;
mysql> start slave;
mysql> show slave status\G;
slave02 192.168.100.170
vim /etc/my.cnf
'//mysqld字段下面修改'
server-id = 23
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
systemctl restart mysqld.service
mysql -uroot -pabc23
Enter password:
mysql> change master to master_host='192.168.100.150',master_user='myslave',master_password='123456',master_log_file='master-bin.000013',master_log_pos=154;
flush privileges;
mysql> start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes '//发现IO是正常的'
Slave_SQL_Running: Yes '//SQL是正常的'
----------------------------------读写分离----------------------------------
###读写分离的前提是已部署主从服务
cd /opt
上传jdk-6u14-linux-x64.bin amoeba-mysql-binary-2.2.0.tar.gz两个文件
cp jdk-6u14-linux-x64.bin /usr/local/
cd /usr/local/
chmod +x jdk-6u14-linux-x64.bin
./jdk-6u14-linux-x64.bin
Do you agree to the above license terms? [yes or no]
yes
mv jdk1.6.0_14/ jdk1.6
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
source /etc/profile
mkdir /usr/local/amoeba
cd /opt
tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
chmod -R 755 /usr/local/amoeba/
/usr/local/amoeba/bin/amoeba
显示amoeba start|stop //说明安装成功
***三台mysql服务器添加权限和用户开放给amoeba访问
mysql> grant all on *.* to 'test'@'192.168.100.%' identified by '123.com';
mysql> flush privileges;
show grants for 'test'@'192.168.100.%';
cd /usr/local/amoeba/conf
****编辑amoeba主配置文件
vim amoeba.xml
//第30行修改
30 <property name="user">amoeba</property> //客户访问amoeba账户
31
32 <property name="password">123456</property> //使用123456密码访问amoeba服务器'
//第115行修改
//开启读写功能池设定'
115 <property name="defaultPool">master</property> //修改默认地址池为master
116
117 <!-- --> '//117行取消注释'
118 <property name="writePool">master</property> //修改写为master
119 <property name="readPool">slaves</property> //修改读为slaves
120 //120行删除-->注释符号
'//编辑数据库配置文件'
vim dbServers.xml
22 <!-- mysql schema -->
23 <property name="schema">mysql</property> '//amoeba默认读取的库将test修改为mysql;23行test修改为mysql' 5.5版本这里是test,原本有这个库,5.7没有自带这个test库,所以要修改
24
//'设置amoeba访问mysql数据库的用户和密码'
25 <!-- mysql user -->
26 <property name="user">test</property> '//26行修改用户名'
27
28 <!-- mysql password --> '//28行-30行取消注释'
29 <property name="password">123.com</property> '//29行修改密码'
45 <dbServer name="master" parent="abstractServer"> '//45行主mysql服务器名称修改为master'
46 <factoryConfig>
47 <!-- mysql ip -->
48 <property name="ipAddress">192.168.100.150</property> '48行//修改主服务器IP'
49 </factoryConfig>
50 </dbServer>
51
52 <dbServer name="slave1" parent="abstractServer"> '//52行修改从服务器1的名称'
53 <factoryConfig>
54 <!-- mysql ip -->
55 <property name="ipAddress">192.168.100.160</property> '//修改从服务器1的地址'
56 </factoryConfig>
57 </dbServer>
58 '将上面从服务器配置的7行复制下来修改名称slave2和ip地址'
59 <dbServer name="slave2" parent="abstractServer"> '//修改从服务器2的名称'
60 <factoryConfig>
61 <!-- mysql ip -->
62 <property name="ipAddress">192.168.100.170</property> '//修改从服务器1的地址
63 </factoryConfig>
64 </dbServer>
65 '//修改数据库从服务器池'
66 <dbServer name="slaves" virtual="true"> //修改服务器的名称为slaves'
67 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
68 <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
69 <property name="loadbalance">1</property>
70
71 <!-- Separated by commas,such as: server1,server2,server1 -->
72 <property name="poolNames">slave1,slave2</property> '//添加两个从服务器名称slave1,slave2'
73 </poolConfig>
74 </dbServer>
开启amoeba软件,放到后台运行
[root@amoeba conf]# /usr/local/amoeba/bin/amoeba start &
[3] 11060
[root@amoeba conf]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2020-08-27 00:26:27,797 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2020-08-27 00:26:27,916 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066.
2020-08-27 00:26:27,916 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:5790.
'//此时处于持续监控状态,无法输入命令,但是不要关闭,在连接一个窗口看端口有没有开启'
[root@amoeba ~]# netstat -ntap | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 11060/java
--------------------------------------------客户端安装mysql验证--------------------------------------------[root@localhost ~]# yum install -y mysql
systemctl stop firewalld.service
setenforce 0
yum install mysql -y
mysql -u amoeba -pabc123 -h 192.168.100.130 -P8066
***测试读写分离,关闭主从复制功能
两台从服务器关闭slave功能
stop slave;
show slave status\G;
...省略内容
Slave_IO_Running: No '//发现功能已被关闭'
Slave_SQL_Running: No
...省略内容
***从服务器配置数据,验证读写分离
客户机
MySQL [(none)]> create database accp;
Query OK, 1 row affected (0.02 sec)
mysql> show databases; //主服务器能看见accp库
+--------------------+
| Database |
+--------------------+
| information_schema |
| accp |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases; //两台从服务器都看不见accp库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
'//会发现主服务器有,但是从服务器没有,说明主从复制功能已经关闭'
***验证读分离
mysql> use mysql;
Database changed
mysql> create table info (id int not null,name char(10));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into info values(1,'zhangsan'); '//添加zhangsan记录'
Query OK, 1 row affected (0.02 sec)
mysql> select * from info;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
'//slave1从服务器设置:'
mysql> use mysql;
Database changed
mysql> create table info(id int not null,name char(10));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into info values(2,'lisi'); '//添加lisi记录'
Query OK, 1 row affected (0.02 sec)
mysql> select * from info;
+----+------+
| id | name |
+----+------+
| 2 | lisi |
+----+------+
1 row in set (0.00 sec)
****客户端测试
MySQL [(none)]> use mysql;
MySQL [mysql]> select * from info;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
MySQL [mysql]> select * from info;
+----+------+
| id | name |
+----+------+
| 2 | lisi |
+----+------+
1 row in set (0.01 sec)
'//发现每一次查询都会轮询在slave1和slave2上查询数据,如果开启主从复制,则数据都会相同'
'//读写分离试验成功'
版权声明
本文为[osc_tzzfwr6n]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4414570/blog/4711215

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云