Openresty notes-access memcache and mysql

Source: Internet
Author: User
: This article mainly introduces openresty notes-accessing memcache and mysql. if you are interested in PHP tutorials, refer to it.
worker_processes  1;                                                                                                                         error_log logs/error.log;events {    worker_connections 1024;}http {    server {        listen 8080;        location /hello_world {            default_type text/html;            content_by_lua '                ngx.say("

hello, world

") '; } location /memcache_test {default_type text/plain;content_by_lua '--argslocal args = ngx.req.get_uri_args()if (args["key"] == nil) thenreturnend--newlocal memcached = require "resty.memcached"local memc , err = memcached:new()if not memc then ngx.say("faild to init memc:" , err)returnendmemc:set_timeout(1000)--connectlocal ok , err = memc:connect("127.0.0.1" , 11211)if not ok then ngx.say("faild to connect:" , err)returnend--getlocal res , flags , err = memc:get(args["key"])if err thenngx.say("faild to get: " , err)returnendif res thenngx.say("get key [" .. args["key"] .. "] " .. "[" .. res .. "]")elsengx.say("not get key [" .. args["key"] .. "]")end--setlocal ok , err = memc:set(args["key"] , args["key"])if ok thenngx.say("set ok")end'; }location /mysql_test{default_type text/plain;content_by_lua 'local mysql = require "resty.mysql"--newlocal db , err = mysql:new()if not db then ngx.say("faild to init mysql: " , err)returnenddb:set_timeout(1000)--connectlocal ok , err , errno , sqlstate = db:connect{host = "127.0.0.1",port = 3306,database = "test",user = "root",password = "123456",max_packet_size = 1024 * 1024}if not ok thenngx.say("failed to connect to mysql : ", err, ": ", errno, " ", sqlstate)returnendngx.say("connected to mysql")--create tablelocal sql = [[create table if not exists `test`(`id` int(11) primary key not null auto_increment,`name` varchar(100) not null default \'\') engine=Innodb default charset=utf8]]local res , err , errno , sqlstate = db:query(sql)if not res thenngx.say("create table error: ", err, ": ", errno, ": ", sqlstate, ".")returnend--insertsql = "insert into `test` (`name`) values (\'fish\')"local res , err , errno , sqlstate = db:query(sql)if not res thenngx.say("insert error: ", err, ": ", errno, ": ", sqlstate, ".")returnend--selectsql = "select * from `test"`local res , err , errno , sqlstate = db:query(sql)if not res thenngx.say("select error: ", err, ": ", errno, ": ", sqlstate, ".")returnendlocal cjson = require "cjson"ngx.say(cjson.encode(res))--deletesql = "delete from `test"`local res , err , errno , sqlstate = db:query(sql)if not res thenngx.say("delete error: ", err, ": ", errno, ": ", sqlstate, ".")returnend';} }}

The above introduces the openresty notes-accessing memcache and mysql, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.