Nginx實現非同步訪問mysql的配置方法_nginx

來源:互聯網
上載者:User

nginx中有一個模組有這個功能,(以前的文章中擴充,這裡叫模組,以後統一叫模組,模組可能準確點,因為擴充是單獨的檔案,而模組是嵌入到主檔案中的),這個模組叫drizzle-nginx-module,下載地址為:

https://github.com/openresty/drizzle-nginx-module

要編譯這個還不容易,上面這個地址也有說明:

先下載drizzle庫,地址:http://openresty.org/download/drizzle7-2011.07.21.tar.gz
編譯和配置選項:

複製代碼 代碼如下:

./configure --without-server
    make libdrizzle-1.0
    make install-libdrizzle-1.0

接下是輸出,http直接輸出json字串,怎麼辦,又要下一個模組,要下的模組太多了,
https://github.com/openresty/rds-json-nginx-module

最終的配置選項如下:

複製代碼 代碼如下:

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

最終啟動nginx的時候,還有可能報drizzle共用庫找不到,可以看下設定檔:/etc/ld.so.conf,改完配置後,記得要用lddconfig重新生效下

如何使用呢

首先在nginx的http模組中聲明一個mysql的server

複製代碼 代碼如下:

upstream backend {
        drizzle_server 172.21.107.247:3306 dbname=oneplus_user_sso password= user=root protocol=mysql;
    }

接下來是聲明location處理了
複製代碼 代碼如下:

        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;
        }


其中rds_json on表示輸出為json格式

接下來,訪問地址:http://172.21.107.174/mysql,
就可以輸出一個使用者的資訊,並且格式是json的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.