{"id":1529,"date":"2014-10-23T14:51:03","date_gmt":"2014-10-23T14:51:03","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1529"},"modified":"2014-10-23T14:51:03","modified_gmt":"2014-10-23T14:51:03","slug":"python%e7%9a%84mysqldb%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1529.html","title":{"rendered":"Python\u7684MySQLdb\u6a21\u5757"},"content":{"rendered":"<h5><strong>\u5728<\/strong><strong>Ubuntu<\/strong><strong>\u4e0b\u5b89\u88c5<\/strong><strong>MySQLdb<\/strong><strong>\u6a21\u5757<\/strong><\/h5>\n<p>\u56e0\u4e3a\u8981\u4f7f\u7528Python\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u6240\u4ee5\u9700\u8981\u5b89\u88c5MySQLdb\u6a21\u5757\uff0c\u4f46\u662f\u7b2c\u4e00\u6b21\u5728Ubuntu\u4e0b\u5b89\u88c5\u7684\u65f6\u5019\u5931\u8d25\u4e86\uff0c\u4f46\u540e\u6765\u6211\u60f3\u7740\u4ee5\u540e\u53ef\u80fd\u4f1a\u7ecf\u5e38\u9700\u8981\uff0c\u6240\u4ee5\u8fd8\u662f\u5f97\u5b89\u88c5\u597d\u4e86\uff0c\u4ee5\u540e\u624d\u4f1a\u65b9\u4fbf\u4e00\u4e9b\u3002<\/p>\n<ul>\n<li><strong>\u5b89\u88c5<\/strong><strong>PIP<\/strong><\/li>\n<\/ul>\n<p><a href=\"http:\/\/pip.readthedocs.org\/en\/latest\/installing.html\">http:\/\/pip.readthedocs.org\/en\/latest\/installing.html<\/a><\/p>\n<pre class=\"lang:default decode:true\">wget --no-check-certificate https:\/\/bootstrap.pypa.io\/get-pip.py\npython get-pip.py<\/pre>\n<ul>\n<li><strong>\u5b89\u88c5<\/strong><strong>MySQLdb<\/strong><\/li>\n<\/ul>\n<p>\u5148\u4f7f\u7528PIP\u5b89\u88c5\uff0c\u62a5\u9519\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># pip install MySQLdb\nDownloading\/unpacking MySQLdb\n\u00a0 Real name of requirement MySQLdb is mysqldb\n\u00a0 Could not find any downloads that satisfy the requirement MySQLdb\nCleaning up...\nNo distributions at all found for MySQLdb\nStoring debug log for failure in \/root\/.pip\/pip.log<\/pre>\n<p>\u7136\u540e\u5c1d\u8bd5\u6e90\u7801\u5b89\u88c5\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># wget --no-check-certificate https:\/\/pypi.python.org\/packages\/source\/M\/MySQL-python\/MySQL-python-1.2.5.zip\n# unzip MySQL-python-1.2.5.zip<\/pre>\n<p><strong>\u9519\u8bef1<\/strong><strong>\uff1a<\/strong><\/p>\n<p># python setup.py install<br \/>\nsh: 1: mysql_config: not found<br \/>\nTraceback (most recent call last):<br \/>\nFile &#8220;setup.py&#8221;, line 17, in &lt;module&gt;<br \/>\nmetadata, options = get_config()<br \/>\nFile &#8220;\/root\/downLoads\/MySQL-python-1.2.5\/setup_posix.py&#8221;, line 43, in get_config<br \/>\nlibs = mysql_config(&#8220;libs_r&#8221;)<br \/>\nFile &#8220;\/root\/downLoads\/MySQL-python-1.2.5\/setup_posix.py&#8221;, line 25, in mysql_config<br \/>\nraise EnvironmentError(&#8220;%s not found&#8221; % (mysql_config.path,))<br \/>\n<span style=\"color: #ff0000;\"><strong>EnvironmentError: mysql_config not found<\/strong><\/span><\/p>\n<p><strong>\u89e3\u51b3\u529e\u6cd51<\/strong><strong>\uff1a<\/strong><\/p>\n<p>\u5b89\u88c5libmysqld-dev\uff08# apt-get install libmysqld-dev\uff09<\/p>\n<p><strong>\u9519\u8bef2<\/strong><strong>\uff1a<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">_mysql.c:29:20: fatal error: Python.h: No such file or directory<\/span><\/strong><br \/>\n#include &#8220;Python.h&#8221;<br \/>\n^<br \/>\ncompilation terminated.<br \/>\nerror: command &#8216;i686-linux-gnu-gcc&#8217; failed with exit status 1<\/p>\n<p><strong>\u89e3\u51b3\u529e\u6cd52<\/strong><strong>\uff1a<\/strong><\/p>\n<p>\u5b89\u88c5python-dev\uff08# apt-get install python2.7-dev\uff09<\/p>\n<p>\u7136\u540e\u518d\u6b21\u5b89\u88c5\u7684\u65f6\u5019\uff0c\u867d\u7136\u4e2d\u9014\u6709warning\uff0c\u4f46\u662f\u5df2\u7ecf\u4e0d\u788d\u4e8b\u4e86\uff1a<\/p>\n<p>Adding MySQL-python 1.2.5 to easy-install.pth file<br \/>\nInstalled \/usr\/local\/lib\/python2.7\/dist-packages\/MySQL_python-1.2.5-py2.7-linux-i686.egg<br \/>\nProcessing dependencies for MySQL-python==1.2.5<br \/>\nFinished processing dependencies for MySQL-python==1.2.5<\/p>\n<p>\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<h6><strong>\u53c2\u8003\u94fe\u63a5\uff1a<\/strong><\/h6>\n<ul>\n<li><a href=\"http:\/\/zhoujianghai.iteye.com\/blog\/1520666\" target=\"_blank\">python\u5b66\u4e60\u7b14\u8bb0-\u5b89\u88c5mysql-python\uff1aEnvironmentError: mysql_config not found<\/a><\/li>\n<\/ul>\n<h5>\u5728Windows\u4e0a\u5b89\u88c5MySQLdb<\/h5>\n<p>\u5982\u679c\u4f60\u5b89\u88c5\u4e86VS2010\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u53ef\u4ee5\u8003\u8651\u6e90\u7801\u5b89\u88c5\uff0c\u5982\u679c\u56fe\u65b9\u4fbf\u7684\u8bdd\uff0c\u5c31\u53bb\u4e0b\u8f7d\u522b\u4eba\u7f16\u8bd1\u597d\u7684exe\u7136\u540e\u5b89\u88c5\u5427\u3002{<a href=\"http:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#mysql-python\" target=\"_blank\">http:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#mysql-python<\/a>}<\/p>\n<hr \/>\n<h5><strong>\u4f7f\u7528<\/strong><strong>Python<\/strong><strong>\u64cd\u4f5c<\/strong><strong>MySQLdb<\/strong><strong>\u7684\u6837\u4f8b<\/strong><\/h5>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; import MySQLdb\n&gt;&gt;&gt; conn = MySQLdb.connect(host=\"localhost\", user=\"root\", passwd=\"123123\", db=\"test\", charset=\"utf8\")\u00a0 #localhost\u6709\u65f6\u9700\u8981\u5199\u6210127.0.0.1\uff0c\u5426\u5219\u4f1a\u62a5\u9519\uff0c\u5177\u4f53\u539f\u56e0\u4e0d\u8be6\n&gt;&gt;&gt; cur = conn.cursor()\n\n&gt;&gt;&gt; cur.execute(\"insert into users (username,password,email) values (%s,%s,%s)\", (\"python\",\"123456\",\"python@gmail.com\"))\n&gt;&gt;&gt; conn.commit()\n\n&gt;&gt;&gt; cur.executemany(\"insert into users (username,password,email) values (%s,%s,%s)\",((\"google\",\"111222\",\"g@gmail.com\"),(\"facebook\",\"222333\",\"f@face.book\"),(\"github\",\"333444\",\"git@hub.com\"),(\"docker\",\"444555\",\"doc@ker.com\")))\n4L\n&gt;&gt;&gt; conn.commit()<\/pre>\n<h6><strong>\u53c2\u8003\u94fe\u63a5\uff1a<\/strong><\/h6>\n<ul>\n<li><a href=\"http:\/\/segmentfault.com\/blog\/qiwsir\/1190000000709735\" target=\"_blank\">[\u96f6\u57fa\u7840\u5b66python]\u7528Python\u64cd\u4f5c\u6570\u636e\u5e93\uff081\uff09<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/qiwsir\/ITArticles\/blob\/master\/BasicPython\/305.md\" target=\"_blank\">https:\/\/github.com\/qiwsir\/ITArticles\/blob\/master\/BasicPython\/305.md<\/a><\/li>\n<\/ul>\n<hr \/>\n<p><strong>Windows<\/strong><strong>\u4e0b<\/strong><strong>MySQL<\/strong><strong>\u5fd8\u8bb0\u5bc6\u7801\u7684\u5904\u7406\u529e\u6cd5<\/strong><\/p>\n<p>Console1:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mysqld &#8211;skip-grant-tables<\/p>\n<p>Console2:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mysql -uroot -p<\/p>\n<pre class=\"lang:default decode:true\">update mysql.user set password=PASSWORD('xxx') where User='root';\nflush privileges;<\/pre>\n<p>=========Login again=============<\/p>\n<ul>\n<li><a href=\"http:\/\/hi.baidu.com\/sppeivan\/item\/358e6fc07e9620320831c621\" target=\"_blank\">Mysql\u5b57\u7b26\u96c6\u8bbe\u7f6e\u8be6\u89e3<\/a><\/li>\n<li><a href=\"http:\/\/www.ha97.com\/5359.html\" target=\"_blank\">\uff08\u539f\u521b\uff09Linux\u4e0bMySQL 5.5\u7684\u4fee\u6539\u5b57\u7b26\u96c6\u7f16\u7801\u4e3aUTF8\uff08\u5f7b\u5e95\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff09<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Ubuntu\u4e0b\u5b89\u88c5MySQLdb\u6a21\u5757 \u56e0\u4e3a\u8981\u4f7f\u7528Python\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u6240\u4ee5\u9700\u8981\u5b89\u88c5MySQLdb\u6a21\u5757\uff0c\u4f46\u662f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,12],"tags":[370,8],"class_list":["post-1529","post","type-post","status-publish","format-standard","hentry","category-database","category-tools","tag-mysqldb","tag-python"],"views":5286,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1529"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}