{"id":194537,"date":"2024-11-13T07:29:19","date_gmt":"2024-11-12T23:29:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194537\/"},"modified":"2024-11-13T07:29:20","modified_gmt":"2024-11-12T23:29:20","slug":"%e7%94%a8mono%e4%be%86%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%a8linux%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194537\/","title":{"rendered":"\u7528Mono\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u5728Linux\u4e0a"},"content":{"rendered":"<h1 id=\"%e7%94%a8mono%e4%be%86%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%a8linux%e4%b8%8a-hQvInwShEH\">\u7528Mono\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u5728Linux\u4e0a<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u958b\u767c\u74b0\u5883\u4e2d\uff0c\u4f7f\u7528Mono\u4f86\u958b\u767c\u8de8\u5e73\u53f0\u7684\u61c9\u7528\u7a0b\u5e8f\u5df2\u7d93\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002Mono\u662f\u4e00\u500b\u958b\u6e90\u7684.NET\u6846\u67b6\u5be6\u73fe\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728Linux\u7b49\u975eWindows\u5e73\u53f0\u4e0a\u904b\u884c.NET\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Linux\u4e0a\u4f7f\u7528Mono\u9023\u63a5MySQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u6b65\u9a5f\u548c\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-hQvInwShEH\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684Linux\u7cfb\u7d71\u4e0a\u5df2\u7d93\u5b89\u88dd\u4e86Mono\u548cMySQL\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd\u9019\u4e9b\u7d44\u4ef6\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dmono-hQvInwShEH\">\u5b89\u88ddMono<\/h3>\n<pre><code>sudo apt update\nsudo apt install mono-complete<\/code><\/pre>\n<p>\u9019\u5c07\u5b89\u88ddMono\u7684\u5b8c\u6574\u7248\u672c\uff0c\u5305\u62ec\u6240\u6709\u5fc5\u8981\u7684\u5eab\u548c\u5de5\u5177\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dmysql-hQvInwShEH\">\u5b89\u88ddMySQL<\/h3>\n<pre><code>sudo apt install mysql-server<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5MySQL\u670d\u52d9\uff1a<\/p>\n<pre><code>sudo service mysql start<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e7%bd%aemysql%e6%95%b8%e6%93%9a%e5%ba%ab-hQvInwShEH\">\u8a2d\u7f6eMySQL\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u9023\u63a5MySQL\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u6578\u64da\u5eab\u548c\u7528\u6236\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u6578\u64da\u5eab\u548c\u7528\u6236\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>mysql -u root -p\nCREATE DATABASE testdb;\nCREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';\nGRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';\nFLUSH PRIVILEGES;\nEXIT;<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba\u201ctestdb\u201d\u7684\u6578\u64da\u5eab\u548c\u4e00\u500b\u540d\u70ba\u201ctestuser\u201d\u7684\u7528\u6236\uff0c\u4e26\u70ba\u8a72\u7528\u6236\u8a2d\u7f6e\u4e86\u5bc6\u78bc\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8mono%e9%80%a3%e6%8e%a5mysql-hQvInwShEH\">\u4f7f\u7528Mono\u9023\u63a5MySQL<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u4f7f\u7528C#\u7de8\u5beb\u4e00\u500b\u7c21\u55ae\u7684\u61c9\u7528\u7a0b\u5e8f\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88ddMySQL\u7684.NET\u9023\u63a5\u5668\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt install libmysqlclient-dev\nsudo apt install mono-devel<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u65b0\u7684C#\u6587\u4ef6\uff0c\u4f8b\u5982\u201cMySqlConnectionExample.cs\u201d\uff0c\u4e26\u7de8\u5beb\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>using System;\nusing MySql.Data.MySqlClient;\n\nclass Program\n{\n    static void Main()\n    {\n        string connectionString = \"Server=localhost;Database=testdb;User ID=testuser;Password=password;\";\n        \n        using (MySqlConnection conn = new MySqlConnection(connectionString))\n        {\n            try\n            {\n                conn.Open();\n                Console.WriteLine(\"\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff01\");\n\n                \/\/ \u57f7\u884c\u67e5\u8a62\n                MySqlCommand cmd = new MySqlCommand(\"SELECT * FROM your_table_name\", conn);\n                MySqlDataReader reader = cmd.ExecuteReader();\n\n                while (reader.Read())\n                {\n                    Console.WriteLine(reader[\"column_name\"].ToString());\n                }\n                reader.Close();\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(\"\u9023\u63a5\u5931\u6557: \" + ex.Message);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u9023\u63a5\u5b57\u7b26\u4e32\uff0c\u7136\u5f8c\u4f7f\u7528MySqlConnection\u4f86\u6253\u958b\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u63a5\u8457\uff0c\u6211\u5011\u57f7\u884c\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u67e5\u8a62\u4e26\u8f38\u51fa\u7d50\u679c\u3002<\/p>\n<h2 id=\"%e7%b7%a8%e8%ad%af%e5%92%8c%e9%81%8b%e8%a1%8c%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-hQvInwShEH\">\u7de8\u8b6f\u548c\u904b\u884c\u61c9\u7528\u7a0b\u5e8f<\/h2>\n<p>\u8981\u7de8\u8b6f\u548c\u904b\u884c\u9019\u500b\u61c9\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>mcs -r:MySql.Data.dll MySqlConnectionExample.cs\nmono MySqlConnectionExample.exe<\/code><\/pre>\n<p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u60a8\u61c9\u8a72\u6703\u770b\u5230\u201c\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff01\u201d\u7684\u6d88\u606f\uff0c\u4e26\u4e14\u67e5\u8a62\u7d50\u679c\u5c07\u88ab\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-hQvInwShEH\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728Linux\u4e0a\u4f7f\u7528Mono\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u3002\u5f9e\u74b0\u5883\u6e96\u5099\u5230\u7de8\u5beb\u548c\u904b\u884cC#\u4ee3\u78bc\uff0c\u6211\u5011\u6db5\u84cb\u4e86\u6240\u6709\u5fc5\u8981\u7684\u6b65\u9a5f\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u9069\u7528\u65bc\u958b\u767c\u5c0f\u578b\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e5f\u53ef\u4ee5\u64f4\u5c55\u5230\u66f4\u8907\u96dc\u7684\u7cfb\u7d71\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728Linux\u4e0a\u4f7f\u7528Mono\u9023\u63a5MySQL\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u548c\u61c9\u7528\u958b\u767c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-194537","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=194537"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194537\/revisions"}],"predecessor-version":[{"id":194538,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194537\/revisions\/194538"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}