您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何進行OpenResty連接Mysql,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
Centos系統下安裝mysql,先下載mysql-community-release-el7-5.noarch.rpm,然后通過yum安裝,安裝過程一直確定【Y】即可。
cd /usr/downloads/ wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum install mysql-community-server
安裝成功后,重啟mysql,并進入mysql數據庫,給root用戶設置一個密碼,密碼為“123”。
service mysqld restart mysql -u root -p set password for root@localhost = password('123');
lua-resty-mysql模塊的官方文檔地址: https://github.com/openresty/lua-resty-mysql
lua-resty-mysql - Lua MySQL client driver for ngx_lua based on the cosocket API
lua-resty-mysql模塊是基于cosocket API 為ngx_lua提供的一個Lua MySQL客戶端。它保證了100%非阻塞。
vim /usr/example/lua/test_mysql.lua,添加以下的代碼:
local function close_db(db) if not db then return end db:close() end local mysql = require("resty.mysql") local db, err = mysql:new() if not db then ngx.say("new mysql error : ", err) return end db:set_timeout(1000) local props = { host = "127.0.0.1", port = 3306, database = "mysql", user = "root", password = "123" } local res, err, errno, sqlstate = db:connect(props) if not res then ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end local drop_table_sql = "drop table if exists test" res, err, errno, sqlstate = db:query(drop_table_sql) if not res then ngx.say("drop table error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end local create_table_sql = "create table test(id int primary key auto_increment, ch varchar(100))" res, err, errno, sqlstate = db:query(create_table_sql) if not res then ngx.say("create table error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end local insert_sql = "insert into test (ch) values('hello')" res, err, errno, sqlstate = db:query(insert_sql) if not res then ngx.say("insert error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end res, err, errno, sqlstate = db:query(insert_sql) ngx.say("insert rows : ", res.affected_rows, " , id : ", res.insert_id, "<br/>") local update_sql = "update test set ch = 'hello2' where id =" .. res.insert_id res, err, errno, sqlstate = db:query(update_sql) if not res then ngx.say("update error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end ngx.say("update rows : ", res.affected_rows, "<br/>") local select_sql = "select id, ch from test" res, err, errno, sqlstate = db:query(select_sql) if not res then ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end for i, row in ipairs(res) do for name, value in pairs(row) do ngx.say("select row ", i, " : ", name, " = ", value, "<br/>") end end ngx.say("<br/>") local ch_param = ngx.req.get_uri_args()["ch"] or '' local query_sql = "select id, ch from test where ch = " .. ngx.quote_sql_str(ch_param) res, err, errno, sqlstate = db:query(query_sql) if not res then ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end for i, row in ipairs(res) do for name, value in pairs(row) do ngx.say("select row ", i, " : ", name, " = ", value, "<br/>") end end local delete_sql = "delete from test" res, err, errno, sqlstate = db:query(delete_sql) if not res then ngx.say("delete error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end ngx.say("delete rows : ", res.affected_rows, "<br/>") close_db(db)
在上面的代碼中,展示了基本的創表、插入數據、修改數據、查詢數據、刪除數據的一些功能。
其中用到的lua-resty-mysql的一些API方法:
syntax: db, err = mysql:new() 創建一個mysql數據庫連接對象
syntax: ok, err = db:connect(options) 嘗試遠程連接mysql
host mysql的主機名
port 端口
database 數據庫名
user 用戶名
password 密碼
charset 編碼
syntax: db:set_timeout(time) 設置數據庫連接超時時間
syntax: ok, err = db:set_keepalive(max_idle_timeout, pool_size) 設置連接池
syntax: ok, err = db:close() 關閉數據庫
syntax: bytes, err = db:send_query(query) 發送查詢
lua-resty-mysql的一些關鍵的API方法,見https://github.com/openresty/lua-resty-mysql#table-of-contents
vim /usr/example/example.conf 在配置文件配置:
location /lua_mysql { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/example/lua/test_mysql.lua; }
瀏覽器訪問http://116.196.177.123/lua_mysql,瀏覽器顯示以下的內容:
insert rows : 1 , id : 2 update rows : 1 select row 1 : ch = hello select row 1 : id = 1 select row 2 : ch = hello2 select row 2 : id = 2 delete rows : 2
上述內容就是如何進行OpenResty連接Mysql,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。