How to use nginx as a load balancer for mysql

How to use nginx as a load balancer for mysql

Note: The nginx version must be 1.9 or above. When compiling nginx, you need to add --with-stream

like:

./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

Notice

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

2. Ensure that the root user can remotely connect to MySQL

For example, the mysql table user in the database

nginx.conf

This code is appended to the end of the nginx.conf file. Note that it cannot be added within 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 one that mysql listens to proxy_pass db;
}
upstream db {
server 127.0.0.1:3306;
server 192.168.233.1:3306;
}

Restart nginx and check whether nginx is listening on port 3307

Then the PHP code is like this

#In fact, when creating new mysqli, you only need to change the port number to the same as the port number set by nginx reverse proxy. $mysqli = new mysqli('127.0.0.1','root','root','test',3307);

Complete PHP code

<?php
class MysqlClass
{
private static $obj = NULL; //mysqlclass object public $host;
public $database;
public $user;
public $pwd;
public $port;
public $mysqli = NULL;
//Prohibit objects from being cloned private function __clone(){}
//Prohibit external instantiation private function __construct($host="127.0.0.1",$database="test",$user="root",$pwd="root",$port="3307")
{
$this->host = $host;
$this->database = $database;
$this->user = $user;
$this->pwd = $pwd;
$this->port = $port;
$this->mysqli = $this->db_connect();
}
//Get mysqli connection private function db_connect()
{
$mysqli = new mysqli($this->host,$this->user,$this->pwd,$this->database,$this->port);
if($mysqli->connect_errno)
{
printf("Connect failed: %s\n", $mysqli->connect_errno);
exit();
}
$mysqli->query("set names utf8 ");
return $mysqli;
}
//Get the db instance public static function get_db()
{
if(self::$obj === NULL)
{
self::$obj = new self();
}
return self::$obj;
}
public function db_query($sql)
{
$result = $this->mysqli->query($sql);
$arr = [];
while ($row = $result->fetch_assoc()) {
$arr[] = $row;
}
$result->close();
$this->mysqli->close();
return $arr;
}
public function db_insert()
{
}
public function db_update()
{
}
public function __destruct() {
$this->mysqli->close();
}
}
$db = MysqlClass::get_db();
$r = $db->db_query("show tables");
var_dump($r);

result

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the implementation steps of MySQL dual-machine hot standby and load balancing
  • Method of using MySQL system database for performance load diagnosis
  • How to implement load balancing in MySQL
  • Implement MySQL read-write separation and load balancing based on OneProxy
  • Build a stable and highly available cluster based on mysql+mycat, load balancing, master-slave replication, read-write separation operation
  • Python implements MySQL read-write separation and load balancing
  • Keepalived+HAProxy to implement MySQL high availability load balancing configuration
  • Analyze the CPU load surge caused by indexes in MySQL
  • How to quickly increase the load capacity of MYSQL database connections
  • Deployment and implementation of MySQL server cluster with load balancing function
  • Troubleshooting MySQL high CPU load issues

<<:  Solve the problem of Chinese garbled characters when inserting data into MySQL by Tomcat under Linux

>>:  How to use JS WebSocket to implement simple chat

Recommend

Solve the 1251 error when establishing a connection between mysql and navicat

I reinstalled the computer and installed the late...

Set the width of the table to be fixed so that it does not change with the text

After setting the table width in the page to width...

Detailed explanation of MySQL clustered index and non-clustered index

1. Clustered Index Table data is stored in the or...

How to monitor array changes in Vue

Table of contents Preface Source code Where do I ...

Implementation of MySQL asc and desc data sorting

Data sorting asc, desc 1. Single field sorting or...

Vue3+TypeScript implements a complete example of a recursive menu component

Table of contents Preface need accomplish First R...

Solve the problem of blank gap at the bottom of Img picture

When working on a recent project, I found that th...

How to handle long data when displaying it in html

When displaying long data in HTML, you can cut off...

Deep understanding of line-height and vertical-align

Several concepts Line box: A box that wraps an in...

MySQL 5.7.17 and workbench installation and configuration graphic tutorial

This article shares the installation and configur...

Setting up shared folders in Ubuntu virtual machine of VMWare14.0.0

This is my first blog post. Due to time constrain...

Use neat HTML markup to build your pages

The Internet is an organism that is constantly ev...

Example code for implementing anti-shake in Vue

Anti-shake: Prevent repeated clicks from triggeri...