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

Summary of the differences between Html, sHtml and XHtml

For example: <u> This has no ending characte...

JavaScript example code to determine whether a file exists

1. Business Scenario I have been doing developmen...

How to install Composer in Linux

1. Download the installation script - composer-se...

Illustration of the process of using FileZilla to connect to the FTP server

When I first started setting up an ftp server on ...

Detailed tutorial on deploying Django project using Docker on centos8

introduction In this article, we will introduce h...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

Vue implements graphic verification code login

This article example shares the specific code of ...

Ubuntu 20.04 connects to wifi (2 methods)

I recently installed Ubuntu 20.04 and found that ...

Basic application methods of javascript embedded and external links

Table of contents Basic application of javascript...

Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Preface: This article is based on the experience ...

Examples of correct use of interface and type methods in TypeScript

Table of contents Preface interface type Appendix...

TypeScript decorator definition

Table of contents 1. Concept 1.1 Definition 1.2 D...