翼度科技»论坛 编程开发 mysql 查看内容

mycat实现mysql基于GITD实现双主双从读写分离master节点高可用

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
架构说明
  1. 10.0.0.18 master节点和10.0.0.22节点互为主
  2. 10.0.0.19 10.0.0.18的slave节点
  3. 10.0.0.22 master节点和10.0.0.19节点互为主
  4. 10.0.0.24 10.0.0.22的slave节点
  5. 10.0.0.23 mycat节点
  6. mysql版本8.0.32
  7. 系统版本:rocky8.4
复制代码
mysql主从搭建
  1. #搭建双主节点
  2. #搭建第一个主10.0.0.18
  3. #注释掉/etc/my.cnf.d/mysql-server.cnf
  4. cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF'
  5. #
  6. # This group are read by MySQL server.
  7. # Use it for options that only the server (but not clients) should see
  8. #
  9. # For advice on how to change settings please see
  10. # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html
  11. # Settings user and group are ignored when systemd is used.
  12. # If you need to run mysqld under a different user or group,
  13. # customize your systemd unit file for mysqld according to the
  14. # instructions in http://fedoraproject.org/wiki/Systemd
  15. #[mysqld]
  16. #datadir=/var/lib/mysql
  17. #socket=/var/lib/mysql/mysql.sock
  18. #log-error=/var/log/mysql/mysqld.log
  19. #pid-file=/run/mysqld/mysqld.pid
  20. #log-bin=/data/mysql/logbin/mysql-bin
  21. EOF
  22. #配置主节点的my.cat配置
  23. cat >/etc/my.cnf<<'EOF'
  24. #
  25. # This group is read both both by the client and the server
  26. # use it for options that affect everything
  27. #
  28. [client-server]
  29. #
  30. # include all files from the config directory
  31. #
  32. !includedir /etc/my.cnf.d
  33. [mysqld]
  34. datadir=/var/lib/mysql
  35. socket=/var/lib/mysql/mysql.sock
  36. log-error=/var/log/mysql/mysqld.log
  37. pid-file=/run/mysqld/mysqld.pid
  38. server-id=18
  39. #read-only
  40. general_log
  41. gtid_mode=ON
  42. enforce_gtid_consistency
  43. log-bin=/data/mysql/logbin/mysql-bin
  44. EOF
  45. #创建存放二进制日志的目录
  46. mkdir -p /data/mysql/logbin/
  47. chown -R mysql.mysql /data
  48. #启动数据库
  49. systemctl enable --now mysqld
  50. #配置账号和授权
  51. mysql
  52. create user 'repluser'@'10.0.0.%' identified by '123456';
  53. grant replication slave on *.* to 'repluser'@'10.0.0.%';
  54. #创建mycat使用的账号
  55. create user 'wbiao'@'10.0.0.%' IDENTIFIED BY '123456';
  56. grant ALL ON hellodb.* TO 'wbiao'@'10.0.0.%';
  57. #搭建第二个主10.0.0.22
  58. #注释掉/etc/my.cnf.d/mysql-server.cnf
  59. cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF'
  60. #
  61. # This group are read by MySQL server.
  62. # Use it for options that only the server (but not clients) should see
  63. #
  64. # For advice on how to change settings please see
  65. # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html
  66. # Settings user and group are ignored when systemd is used.
  67. # If you need to run mysqld under a different user or group,
  68. # customize your systemd unit file for mysqld according to the
  69. # instructions in http://fedoraproject.org/wiki/Systemd
  70. #[mysqld]
  71. #datadir=/var/lib/mysql
  72. #socket=/var/lib/mysql/mysql.sock
  73. #log-error=/var/log/mysql/mysqld.log
  74. #pid-file=/run/mysqld/mysqld.pid
  75. #log-bin=/data/mysql/logbin/mysql-bin
  76. EOF
  77. #配置主节点的my.cat配置
  78. cat >/etc/my.cnf<<'EOF'
  79. #
  80. # This group is read both both by the client and the server
  81. # use it for options that affect everything
  82. #
  83. [client-server]
  84. #
  85. # include all files from the config directory
  86. #
  87. !includedir /etc/my.cnf.d
  88. [mysqld]
  89. datadir=/var/lib/mysql
  90. socket=/var/lib/mysql/mysql.sock
  91. log-error=/var/log/mysql/mysqld.log
  92. pid-file=/run/mysqld/mysqld.pid
  93. server-id=22
  94. #read-only
  95. general_log
  96. gtid_mode=ON
  97. enforce_gtid_consistency
  98. log-bin=/data/mysql/logbin/mysql-bin
  99. EOF
  100. #创建存放二进制日志的目录
  101. mkdir -p /data/mysql/logbin/
  102. chown -R mysql.mysql /data
  103. #启动数据库
  104. systemctl enable --now mysqld
  105. #10.0.0.22指向10.0.0.18
  106. 执行change master to
  107. CHANGE MASTER TO
  108. MASTER_HOST='10.0.0.18',
  109. MASTER_USER='repluser',
  110. MASTER_PASSWORD='123456',
  111. MASTER_PORT=3306,
  112. MASTER_AUTO_POSITION=1;
  113. #开启IO线程和SQL线程
  114. start slave;
  115. #检查状态
  116. show slave status\G
  117. #检查
  118. mysql> select user,host from mysql.user;
  119. +------------------+-----------+
  120. | user             | host      |
  121. +------------------+-----------+
  122. | repluser         | 10.0.0.%  |
  123. | wbiao            | 10.0.0.%  |
  124. | mysql.infoschema | localhost |
  125. | mysql.session    | localhost |
  126. | mysql.sys        | localhost |
  127. | root             | localhost |
  128. +------------------+-----------+
  129. 6 rows in set (0.00 sec)
  130. 10.0.0.18指向10.0.0.22
  131. 执行change master to
  132. CHANGE MASTER TO
  133. MASTER_HOST='10.0.0.22',
  134. MASTER_USER='repluser',
  135. MASTER_PASSWORD='123456',
  136. MASTER_PORT=3306,
  137. MASTER_AUTO_POSITION=1;
  138. #开启IO线程和SQL线程
  139. start slave;
  140. #检查状态
  141. show slave status\G
  142. #配置10.0.0.18的从节点10.0.0.19
  143. #注释掉/etc/my.cnf.d/mysql-server.cnf
  144. cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF'
  145. #
  146. # This group are read by MySQL server.
  147. # Use it for options that only the server (but not clients) should see
  148. #
  149. # For advice on how to change settings please see
  150. # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html
  151. # Settings user and group are ignored when systemd is used.
  152. # If you need to run mysqld under a different user or group,
  153. # customize your systemd unit file for mysqld according to the
  154. # instructions in http://fedoraproject.org/wiki/Systemd
  155. #[mysqld]
  156. #datadir=/var/lib/mysql
  157. #socket=/var/lib/mysql/mysql.sock
  158. #log-error=/var/log/mysql/mysqld.log
  159. #pid-file=/run/mysqld/mysqld.pid
  160. #log-bin=/data/mysql/logbin/mysql-bin
  161. EOF
  162. #配置从节点10.0.0.19的my.cat配置
  163. cat >/etc/my.cnf<<'EOF'
  164. #
  165. # This group is read both both by the client and the server
  166. # use it for options that affect everything
  167. #
  168. [client-server]
  169. #
  170. # include all files from the config directory
  171. #
  172. !includedir /etc/my.cnf.d
  173. [mysqld]
  174. datadir=/var/lib/mysql
  175. socket=/var/lib/mysql/mysql.sock
  176. log-error=/var/log/mysql/mysqld.log
  177. pid-file=/run/mysqld/mysqld.pid
  178. server-id=19
  179. read-only
  180. general_log
  181. gtid_mode=ON
  182. enforce_gtid_consistency
  183. log-bin=/data/mysql/logbin/mysql-bin
  184. EOF
  185. #创建存放二进制日志的目录
  186. mkdir -p /data/mysql/logbin/
  187. chown -R mysql.mysql /data
  188. #启动数据库
  189. systemctl enable --now mysqld
  190. #从节点10.0.0.19执行change master to
  191. CHANGE MASTER TO
  192. MASTER_HOST='10.0.0.18',
  193. MASTER_USER='repluser',
  194. MASTER_PASSWORD='123456',
  195. MASTER_PORT=3306,
  196. MASTER_AUTO_POSITION=1;
  197. #开启IO线程和SQL线程
  198. start slave;
  199. #检查状态
  200. show slave status\G
  201. ##配置10.0.0.22的从节点10.0.0.24
  202. #注释掉/etc/my.cnf.d/mysql-server.cnf
  203. cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF'
  204. #
  205. # This group are read by MySQL server.
  206. # Use it for options that only the server (but not clients) should see
  207. #
  208. # For advice on how to change settings please see
  209. # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html
  210. # Settings user and group are ignored when systemd is used.
  211. # If you need to run mysqld under a different user or group,
  212. # customize your systemd unit file for mysqld according to the
  213. # instructions in http://fedoraproject.org/wiki/Systemd
  214. #[mysqld]
  215. #datadir=/var/lib/mysql
  216. #socket=/var/lib/mysql/mysql.sock
  217. #log-error=/var/log/mysql/mysqld.log
  218. #pid-file=/run/mysqld/mysqld.pid
  219. #log-bin=/data/mysql/logbin/mysql-bin
  220. EOF
  221. #配置从节点10.0.0.24的my.cat配置
  222. cat >/etc/my.cnf<<'EOF'
  223. #
  224. # This group is read both both by the client and the server
  225. # use it for options that affect everything
  226. #
  227. [client-server]
  228. #
  229. # include all files from the config directory
  230. #
  231. !includedir /etc/my.cnf.d
  232. [mysqld]
  233. datadir=/var/lib/mysql
  234. socket=/var/lib/mysql/mysql.sock
  235. log-error=/var/log/mysql/mysqld.log
  236. pid-file=/run/mysqld/mysqld.pid
  237. server-id=24
  238. read-only
  239. general_log
  240. gtid_mode=ON
  241. enforce_gtid_consistency
  242. log-bin=/data/mysql/logbin/mysql-bin
  243. EOF
  244. #创建存放二进制日志的目录
  245. mkdir -p /data/mysql/logbin/
  246. chown -R mysql.mysql /data
  247. #启动数据库
  248. systemctl enable --now mysqld
  249. #从节点10.0.0.24执行change master to
  250. CHANGE MASTER TO
  251. MASTER_HOST='10.0.0.22',
  252. MASTER_USER='repluser',
  253. MASTER_PASSWORD='123456',
  254. MASTER_PORT=3306,
  255. MASTER_AUTO_POSITION=1;
  256. #开启IO线程和SQL线程
  257. start slave;
  258. #检查状态
  259. show slave status\G
  260. #10.0.0.18导入hellodb的数据库
  261. [root@10 ~]# mysql <hellodb_innodb.sql
  262. ##检查状态
  263. show slave status\G
  264. #所有节点检查数据
  265. select * from hellodb.students;
  266. #双主只能对一个主进行写操作
复制代码
 

来源:https://www.cnblogs.com/yeyouqing/p/17426909.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具