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:
|
>>: How to use JS WebSocket to implement simple chat
For example: <u> This has no ending characte...
Local Windows remote desktop connects to Alibaba ...
1. Business Scenario I have been doing developmen...
Written in front Weibo components are component p...
1. Download the installation script - composer-se...
When I first started setting up an ftp server on ...
introduction In this article, we will introduce h...
Tab switching is also a common technology in proj...
This article example shares the specific code of ...
I recently installed Ubuntu 20.04 and found that ...
Table of contents Basic application of javascript...
Preface: This article is based on the experience ...
Table of contents Query Background 1. Like query ...
Table of contents Preface interface type Appendix...
Table of contents 1. Concept 1.1 Definition 1.2 D...