Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to access mysql asynchronously by Nginx

Shulou Source: shulou.com Published: 2022-05-31 13:10:34 09月27日 Update

This article mainly explains "how Nginx realizes asynchronous access to mysql". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it together to study and learn "how Nginx realizes asynchronous access to mysql"!

There is a module in nginx that has this function.(In the previous article, the extension is called a module. In the future, it will be called a module. The module may be accurate because the extension is a separate file, and the module is embedded in the main file.) This module is called drizzle-nginx-module.

It's not easy to compile this.

Download drizzle library first

Compile and configure options:

The copy code is as follows:

./ configure --without-server

make libdrizzle-1.0

make install-libdrizzle-1.0

Next is output, http directly output json string, how to do, and the next module, to the next module too many,

https://github.com/openresty/rds-json-nginx-module

The final configuration options are as follows:

The copy code is as follows:

./ configure --prefix=/opt/nginxrw --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --add-module=/data/src/lua-nginx-module-0.9.8 --add-module=/data/src/ngx_devel_kit-0.2.19 --add-module=/data/src/drizzle-nginx-module-0.1.7 --add-module=/data/src/rds-json-nginx-module-master

When you finally start nginx, you may also report that the drizzle shared library cannot be found. You can look at the configuration file: /etc/ld.so.conf. After changing the configuration, remember to use lddconfig to re-take effect.

how to use it

First declare a mysql server in the http module of nginx

The copy code is as follows:

upstream backend {

drizzle_server 172.21.107.247:3306 dbname=oneplus_user_sso password= user=root protocol=mysql;

}

Next up is the declaration that location handles

The copy code is as follows:

location /mysql {

set $my_sql 'select * from t_user limit 1';

drizzle_query $my_sql;

drizzle_pass backend;

drizzle_connect_timeout 500ms; # default 60s

drizzle_send_query_timeout 2s; # default 60s

drizzle_recv_cols_timeout 1s; # default 60s

drizzle_recv_rows_timeout 1s; # default 60s

rds_json on;

}

where rds_json on means output is in json format

Next, visit http://172.21.107.174/mysql,

You can output a user's information, and the format is json.

Thank you for reading, the above is the content of "Nginx how to achieve asynchronous access to mysql", after the study of this article, I believe that everyone has a deeper understanding of how Nginx achieves asynchronous access to mysql, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Module code output configuration file learning next content article format compilation information function address character string that is what to do ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Huawei Shulou Information Redmi