• Nginx Mysql负载均衡 初步配置及验证 笔记


    需求

    两台Mysql服务器(windows),使用Nginx作为反向代理对Mysql做读数据库的负载均衡。

    A机mysql IP端口:192.168.7.161:3306(防火墙已允许该端口通过)

    B机mysql IP端口:192.168.7.162:3306(防火墙已允许该端口通过)

    Nginx所在机器IP端口:192.168.7.161:3333(与A机共用,防火墙已允许该端口通过)

    外界统一通过192.168.7.161:3333访问数据库。

    nginx版本:nginx-1.22.1,测试平台:三台机器都是win7 64.

    安装

    不再赘述,参考本博客文章Nginx 负载均衡 初步配置&验证 笔记_既择远方-风雨兼程的博客-CSDN博客

    Nginx为Mysql配置负载均衡

    配置nginx-1.22.1\conf目录中的nginx.conf文件,记事本打开修改,在文件尾部增加以下关键配置。

    1. #wdh config mysql dbservers proxy
    2. stream{
    3. upstream mysql_dbServers{
    4. server 192.168.7.161:3306;
    5. server 192.168.7.162:3306;
    6. }
    7. server{
    8. listen 3333;
    9. proxy_pass mysql_dbServers;
    10. proxy_connect_timeout 10s;
    11. proxy_timeout 30s;
    12. }
    13. }

    mysql_dbServers节内配置了A机和B机地址及端口供Nginx轮询。server中指定了对外开放3333端口访问nginx-mysql.

    修改完上述配置后保存nginx.conf。

    cmd窗口中cd到nginx目录使用【nginx -s reload】命令重启nginx;

    示例如下

    D:\JavaDevEnv\nginx-1.22.1>nginx -s reload

    完整nginx.conf内容如下(含前述为Web配置的负载均衡参数)

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. #gzip on;
    22. server {
    23. listen 8123;
    24. server_name localhost;
    25. #charset koi8-r;
    26. #access_log logs/host.access.log main;
    27. location / {
    28. root html;
    29. index index.html index.htm;
    30. }
    31. #error_page 404 /404.html;
    32. # redirect server error pages to the static page /50x.html
    33. #
    34. error_page 500 502 503 504 /50x.html;
    35. location = /50x.html {
    36. root html;
    37. }
    38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    39. #
    40. #location ~ \.php$ {
    41. # proxy_pass http://127.0.0.1;
    42. #}
    43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    44. #
    45. #location ~ \.php$ {
    46. # root html;
    47. # fastcgi_pass 127.0.0.1:9000;
    48. # fastcgi_index index.php;
    49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    50. # include fastcgi_params;
    51. #}
    52. # deny access to .htaccess files, if Apache's document root
    53. # concurs with nginx's one
    54. #
    55. #location ~ /\.ht {
    56. # deny all;
    57. #}
    58. }
    59. # another virtual host using mix of IP-, name-, and port-based configuration
    60. #
    61. #server {
    62. # listen 8000;
    63. # listen somename:8080;
    64. # server_name somename alias another.alias;
    65. # location / {
    66. # root html;
    67. # index index.html index.htm;
    68. # }
    69. #}
    70. # HTTPS server
    71. #
    72. #server {
    73. # listen 443 ssl;
    74. # server_name localhost;
    75. # ssl_certificate cert.pem;
    76. # ssl_certificate_key cert.key;
    77. # ssl_session_cache shared:SSL:1m;
    78. # ssl_session_timeout 5m;
    79. # ssl_ciphers HIGH:!aNULL:!MD5;
    80. # ssl_prefer_server_ciphers on;
    81. # location / {
    82. # root html;
    83. # index index.html index.htm;
    84. # }
    85. #}
    86. #wdh config the servers
    87. upstream myhttpIISServer{
    88. server 192.168.7.54:8052;
    89. server 192.168.7.161:8051;
    90. }
    91. #wdh config the proxy
    92. server{
    93. listen 8050;
    94. server_name localhost;
    95. location /{
    96. proxy_pass http://myhttpIISServer;
    97. }
    98. }
    99. }
    100. #wdh config mysql dbservers proxy
    101. stream{
    102. upstream mysql_dbServers{
    103. server 192.168.7.161:3306;
    104. server 192.168.7.162:3306;
    105. }
    106. server{
    107. listen 3333;
    108. proxy_pass mysql_dbServers;
    109. proxy_connect_timeout 10s;
    110. proxy_timeout 30s;
    111. }
    112. }

    测试mysql负载均衡

    测试前的说明

    本次测试使用mysql的root账号,测试前需要将mysql的FromHost权限从默认的localhost提升到不限。

    测试通过对比同一条指令查询到A机B机数据库列表对比查看。

    A机B机的数据库列表数量不一致。A机有11个,B机有7个,A机多。

    无nginx时访问A机

    1. Microsoft Windows [版本 6.1.7601]
    2. 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
    3. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h 192.168.7.161 -P 3306 -u root -p
    4. Enter password: ****
    5. Welcome to the MySQL monitor. Commands end with ; or \g.
    6. Your MySQL connection id is 18
    7. Server version: 5.7.10-log MySQL Community Server (GPL)
    8. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    9. Oracle is a registered trademark of Oracle Corporation and/or its
    10. affiliates. Other names may be trademarks of their respective
    11. owners.
    12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    13. mysql> show databases;
    14. +---------------------+
    15. | Database |
    16. +---------------------+
    17. | information_schema |
    18. | dbdemo |
    19. | hibernatetestdb |
    20. | mysql |
    21. | performance_schema |
    22. | sakila |
    23. | spring_08_06_tx |
    24. | springdatajpatestdb |
    25. | sys |
    26. | tacocloud |
    27. | world |
    28. +---------------------+
    29. 11 rows in set (0.00 sec)
    30. mysql>

    无nginx时访问B机

    1. Microsoft Windows [版本 6.1.7601]
    2. 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
    3. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h 192.168.7.162 -P 3306 -u root -p
    4. Enter password: ****
    5. Welcome to the MySQL monitor. Commands end with ; or \g.
    6. Your MySQL connection id is 15
    7. Server version: 5.7.10-log MySQL Community Server (GPL)
    8. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    9. Oracle is a registered trademark of Oracle Corporation and/or its
    10. affiliates. Other names may be trademarks of their respective
    11. owners.
    12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    13. mysql> show databases;
    14. +--------------------+
    15. | Database |
    16. +--------------------+
    17. | information_schema |
    18. | dbdemo |
    19. | mysql |
    20. | performance_schema |
    21. | sakila |
    22. | sys |
    23. | world |
    24. +--------------------+
    25. 7 rows in set (0.00 sec)
    26. mysql>

    增加Nginx配置后访问nginx3333端口

    测试步骤及结论

    1.cmd窗口通过nginx对外开放的3333端口登录mysql

    mysql -h 192.168.7.161 -P 3333 -u root -p

    1.1.登录mysql后输入命令【show databases;】查看当前数据库列表。(结论:执行两遍返回均为7rows,证明是连接到了B机)

    1.2.在mysql的cmd窗口输入命令【exit;】退出登录(相当于断开当前数据库连接)

    2.第二次在cmd窗口通过nginx对外开放的3333端口登录mysql(相当于新建了一个数据库连接)

    2.1.登录mysql后输入命令【show databases;】查看当前数据库列表。(结论:执行两遍返回均为11rows,证明是连接到了A机)

    2.2在mysql的cmd窗口输入命令【exit;】退出登录(相当于断开当前数据库连接)

    3.第三次在cmd窗口通过nginx对外开放的3333端口登录mysql(相当于新建了一个数据库连接)

    3.1.登录mysql后输入命令【show databases;】查看当前数据库列表。(结论:执行两遍返回均为7rows,证明是连接到了B机)

    3.2在mysql的cmd窗口输入命令【exit;】退出登录(相当于断开当前数据库连接)

    4.第四次在cmd窗口通过nginx对外开放的3333端口登录mysql(相当于新建了一个数据库连接)

    4.1.登录mysql后输入命令【show databases;】查看当前数据库列表。(结论:执行两遍返回均为11rows,证明是连接到了A机)

    4.2在mysql的cmd窗口输入命令【exit;】退出登录(相当于断开当前数据库连接)

    结论:nginx已实现了对mysql的负载均衡,nginx为mysql提供的负载均衡是切换不同的数据库连接。

    测试过程中cmd命令实际记录(连续)

    1. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h 192.168.7.161 -P 3333 -u root -p
    2. Enter password: ****
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 16
    5. Server version: 5.7.10-log MySQL Community Server (GPL)
    6. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql> show databases;
    12. +--------------------+
    13. | Database |
    14. +--------------------+
    15. | information_schema |
    16. | dbdemo |
    17. | mysql |
    18. | performance_schema |
    19. | sakila |
    20. | sys |
    21. | world |
    22. +--------------------+
    23. 7 rows in set (0.00 sec)
    24. mysql> show databases;
    25. +--------------------+
    26. | Database |
    27. +--------------------+
    28. | information_schema |
    29. | dbdemo |
    30. | mysql |
    31. | performance_schema |
    32. | sakila |
    33. | sys |
    34. | world |
    35. +--------------------+
    36. 7 rows in set (0.00 sec)
    37. mysql> exit;
    38. Bye
    39. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h 192.168.7.161 -P 3333 -u root -p
    40. Enter password: ****
    41. Welcome to the MySQL monitor. Commands end with ; or \g.
    42. Your MySQL connection id is 19
    43. Server version: 5.7.10-log MySQL Community Server (GPL)
    44. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    45. Oracle is a registered trademark of Oracle Corporation and/or its
    46. affiliates. Other names may be trademarks of their respective
    47. owners.
    48. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    49. mysql> show databases;
    50. +---------------------+
    51. | Database |
    52. +---------------------+
    53. | information_schema |
    54. | dbdemo |
    55. | hibernatetestdb |
    56. | mysql |
    57. | performance_schema |
    58. | sakila |
    59. | spring_08_06_tx |
    60. | springdatajpatestdb |
    61. | sys |
    62. | tacocloud |
    63. | world |
    64. +---------------------+
    65. 11 rows in set (0.00 sec)
    66. mysql> show databases;
    67. +---------------------+
    68. | Database |
    69. +---------------------+
    70. | information_schema |
    71. | dbdemo |
    72. | hibernatetestdb |
    73. | mysql |
    74. | performance_schema |
    75. | sakila |
    76. | spring_08_06_tx |
    77. | springdatajpatestdb |
    78. | sys |
    79. | tacocloud |
    80. | world |
    81. +---------------------+
    82. 11 rows in set (0.00 sec)
    83. mysql> exit;
    84. Bye
    85. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h 192.168.7.161 -P 3333 -u root -p
    86. Enter password: ****
    87. Welcome to the MySQL monitor. Commands end with ; or \g.
    88. Your MySQL connection id is 17
    89. Server version: 5.7.10-log MySQL Community Server (GPL)
    90. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    91. Oracle is a registered trademark of Oracle Corporation and/or its
    92. affiliates. Other names may be trademarks of their respective
    93. owners.
    94. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    95. mysql> show databases;
    96. +--------------------+
    97. | Database |
    98. +--------------------+
    99. | information_schema |
    100. | dbdemo |
    101. | mysql |
    102. | performance_schema |
    103. | sakila |
    104. | sys |
    105. | world |
    106. +--------------------+
    107. 7 rows in set (0.00 sec)
    108. mysql> show databases;
    109. +--------------------+
    110. | Database |
    111. +--------------------+
    112. | information_schema |
    113. | dbdemo |
    114. | mysql |
    115. | performance_schema |
    116. | sakila |
    117. | sys |
    118. | world |
    119. +--------------------+
    120. 7 rows in set (0.00 sec)
    121. mysql> exit;
    122. Bye
    123. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h 192.168.7.161 -P 3333 -u root -p
    124. Enter password: ****
    125. Welcome to the MySQL monitor. Commands end with ; or \g.
    126. Your MySQL connection id is 20
    127. Server version: 5.7.10-log MySQL Community Server (GPL)
    128. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    129. Oracle is a registered trademark of Oracle Corporation and/or its
    130. affiliates. Other names may be trademarks of their respective
    131. owners.
    132. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    133. mysql> show databases;
    134. +---------------------+
    135. | Database |
    136. +---------------------+
    137. | information_schema |
    138. | dbdemo |
    139. | hibernatetestdb |
    140. | mysql |
    141. | performance_schema |
    142. | sakila |
    143. | spring_08_06_tx |
    144. | springdatajpatestdb |
    145. | sys |
    146. | tacocloud |
    147. | world |
    148. +---------------------+
    149. 11 rows in set (0.00 sec)
    150. mysql> show databases;
    151. +---------------------+
    152. | Database |
    153. +---------------------+
    154. | information_schema |
    155. | dbdemo |
    156. | hibernatetestdb |
    157. | mysql |
    158. | performance_schema |
    159. | sakila |
    160. | spring_08_06_tx |
    161. | springdatajpatestdb |
    162. | sys |
    163. | tacocloud |
    164. | world |
    165. +---------------------+
    166. 11 rows in set (0.00 sec)
    167. mysql>

  • 相关阅读:
    OSCP系列靶场-Esay-Dawn
    Mysql高级20-性能分析
    应用场景丨迭代市政综合管廊监测系统建设
    C++笔记之关于函数名前的取址符
    Java多线程超级详解(看这篇就足够了)
    Vue中使用Switch开关用来控制商品的上架与下架情况、同时根据数据库商品的状态反应到前台、前台修改商品状态保存到数据库
    [附源码]Python计算机毕业设计Django茂名特产销售商城网站
    【SOLIDWORKS学习笔记】工程图基础操作
    字节面经总结
    Tomcat一些漏洞的汇总
  • 原文地址:https://blog.csdn.net/wangdonghao137/article/details/127774014