MySQLdb 连接Mysql 数据库出错解决
- #coding=utf-8
- import MySQLdb
- if __name__ == "__main__":
- db = MySQLdb.connect(host=<span style="color:#FF0000;">'localhost'</span>,
- port=3306,
- user='root',
- passwd=XX',
- db='XX')
- cursor = db.cursor()
- sql = "select * from student"
- cursor.execute(sql)
- for line in cursor.fetchall():
- print line
- db.close()
运行时出现如下错误:
- pydev debugger: starting
- Traceback (most recent call last):
- File "C:\Program Files\aptan3\plugins\org.python.pydev_2.6.0.2012062121\pysrc\pydevd.py", line 1392, in <module>
- debugger.run(setup['file'], None, None)
- File "C:\Program Files\aptan3\plugins\org.python.pydev_2.6.0.2012062121\pysrc\pydevd.py", line 1085, in run
- pydev_imports.execfile(file, globals, locals) #execute the script
- File "D:\Aptana Studio 3 Workspace\first\com\lin\test01.py", line 9, in <module>
- db='netbase')
- File "E:\python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
- return Connection(*args, **kwargs)
- File "E:\python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
- super(Connection, self).__init__(*args, **kwargs2)
- _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
把host="localhost" 改为 host="127.0.0.1"就可以了
- #coding=utf-8
- import MySQLdb
- if __name__ == "__main__":
- db = MySQLdb.connect(<span style="color:#FF0000;">host='127.0.0.1',</span>
- port=3306,
- user='root',
- passwd=XX',
- db='XX')
- cursor = db.cursor()
- sql = "select * from student"
- cursor.execute(sql)
- for line in cursor.fetchall():
- print line
- db.close()
运行如下:
- pydev debugger: starting
- ('lin', 88L)
- ('cjm', 8L)
Django + MySQLdb + Mysql settings 文件数据库设置:
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
- 'NAME': 'mydb', # Or path to database file if using sqlite3.
- # The following settings are not used with sqlite3:
- 'USER': 'root',
- 'PASSWORD': 'mydb',
- #'HOST': '',
- 'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
- 'PORT': '3306', # Set to empty string for default.
- }
- }
然后连接数据库:
- import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
- PyDev console: using default backend (IPython not available).
- E:\python27\python.exe 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
- from django.core import management;import netbase.settings as settings;management.setup_environ(settings)
- u'D:\\Aptana Studio 3 Workspace\\netbase\\netbase'
- from django.db import models
- from django.db import connection
- cursor = connection.cursor()
转:http://blog.csdn.net/jinnian_lin/article/details/10071081
MySQLdb 连接Mysql 数据库出错解决的更多相关文章
- 【php】Apache无法自己主动跳转却显示文件夹与php无法连接mysql数据库的解决方式
一.Apache无法自己主动跳转却显示文件夹 Apache无法自己主动跳转却显示文件夹这是由于Apacheserver下conf/httpd.conf没有配置好,其默认是不跳转,直接显示文件夹 首先, ...
- Mysqldb连接Mysql数据库(转)
python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...
- Mac下php连接mysql数据库失败解决办法
通过phpmyadmin连接mysql成功,但是通过php连接数据库失败,执行如下php语句 ? 1 @mysql_connect("localhost","root&q ...
- 使用spring连接mysql数据库出错
最近在学习spring框架,但是在学到JdbcTemplate时连接数据库一直报错,百度谷歌各种查找都能没有解决问题,简直要癫狂,报错信息如下: org.springframework.jdbc.Ca ...
- python -- MySQLdb连接mysql数据库
1. python安装mysql $ pip install mysql-python 2. 数据库连接程序: import MySQLdb # 打开数据库连接db = MySQLdb.connect ...
- Python3之Django连接mysql数据库
一.mysql服务器 系统版本:CentOS6.8 IP :10.0.0.51 版本 1.授权root远程登录 grant all on *.* to 'root'@'%' identifide by ...
- Django连接MySQL数据库配置
1.自己手动创建数据库 create database 数据库名; # 如: create database bms character set utf8; # 授权访问: grant all pri ...
- python3.4连接mysql数据库的方法
python3.4连接mysql数据库的方法 发布时间:2014-08-04编辑:www.jbxue.com 本文介绍了python3.4连接mysql数据库的方法,在python3.4中不能用mys ...
- Error loading MySQLdb module: No module named 'MySQLdb'----------- django成功连接mysql数据库的方法
在进行django学习过程中,尝试使用框架连接mysql数据库,启动服务器的时候经常遇到Error loading MySQLdb module: No module named 'MySQLdb' ...
随机推荐
- fzu 1909 An Equation(水题)
题目链接:fzu 1909 An Equation 典型的签到题. #include <stdio.h> #include <string.h> bool judge(int ...
- response.sendRedirect 传递参数的问题
response.sendRedirect是通过浏览器来做转向的. 假设在A.jsp页面设置request.setAttribute("username","admin& ...
- 不使用webview,用手机浏览器的android app
需求:wap站在手机上以App的形式打开,但不要嵌套WebView,只能以浏览器打开 package com.gzz.whyinzi; import android.net.Uri; import a ...
- uva 12300 - Smallest Regular Polygon
题意:给定两个点A和B,求包含这两个点的面积最小的正 n(已知)边形. #include<iostream> #include<iomanip> #include<cma ...
- Flume 1.5日志收集和存款mongodb安装结构
Flume该演示是不是说.你可以自己搜索. 但现在的互联网主要是Flume 1.4前版本号的信息.Flume 1.5在轰动的大变化.假设你准备尝试,我在这里给大家介绍一下程序最小化结构,和使用Mong ...
- Android剪切板(ClipboardManager) 复制文本
Android也有剪切板(ClipboardManager) 注意:导包的时候 API 11之前: android.text.ClipboardManagerAPI 11之后: android.c ...
- 在Qt中使用ActiveX控件
Qt的windows商业版本提供了ActiveQt这个framework,使用这个组件我们可以在Qt中使用ActiveX控件,并且也开发基于Qt的ActiveX控件.ActiveQt包含了两个组件QA ...
- mysqldump --flush-logs
<pre name="code" class="html"><pre name="code" class="ht ...
- disruptor流程
这里先不提那些编译器方面的优化.只看一下基于无锁环形队列的生产者消费者模型的工作流程.上一个图先: 当中,buffer是一个数组,用来模拟环形队列. slowest_reader记录最慢的reader ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...