Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nginx as a load balancer for mysql

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Note: the nginx version requires more than 1.9, and you need to add-- with-stream when compiling nginx

Such as:

/ configure-- prefix=/Data/apps/nginx-- with-http_stub_status_module-- with-http_ssl_module-- with-http_realip_module-- with-http_image_filter_module-- with-stream

Be careful

1. Because mysql uses port 3306 by default, when configuring nginx tcp reverse proxy mysql, be careful that the port is not the same as the port that mysql listens on. For example, I use 3307.

two。 Ensure that root users can connect to mysql remotely

Such as database mysql table user

Nginx.conf

This code is appended to the end of the nginx.conf file. Please note that it cannot be added to http {}.

Stream {include / Data/apps/nginx/conf/stream/*.conf;}

Stream/db.conf

Server {listen 3307; # Note that the port cannot be the same as the mysql listeners proxy_pass db;} upstream db {server 127.0.0.1 mysql 3306 server 192.168.233.1 mysql 3306;}

Restart nginx to see if nginx is listening on port 3307

And the php code looks like this.

# in fact, when it is new mysqli, you only need to change the port number to be the same as the port number set by the nginx reverse proxy. $mysqli = new mysqli ('127.0.0.1)

Complete php code

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report