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

Detailed installation process and basic usage of MySQL under Windows

Table of contents 1. Download MySQL 2. Install My...

How to implement the singleton pattern in Javascript

Table of contents Overview Code Implementation Si...

Detailed explanation of Vue element plus multi-language switching

Table of contents Preface How to switch between m...

A method of hiding processes under Linux and the pitfalls encountered

Preface 1. The tools used in this article can be ...

Difference between MySQL btree index and hash index

In MySQL, most indexes (such as PRIMARY KEY, UNIQ...

A simple way to restart QT application in embedded Linux (based on QT4.8 qws)

Application software generally has such business ...

Several common CSS layouts (summary)

Summary This article will introduce the following...

Ubuntu Basic Tutorial: apt-get Command

Preface The apt-get command is a package manageme...

Ideas and codes for implementing Vuex data persistence

What is vuex vuex: is a state manager developed s...

Some settings of Div about border and transparency

frame: Style=”border-style:solid;border-width:5px;...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

JavaScript design pattern learning adapter pattern

Table of contents Overview Code Implementation Su...