当前位置: 首页 > python > 正文

python use mysql

python连接mysql中我遇到了这样的问题

Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
  File “build/bdist.linux-i686/egg/MySQLdb/__init__.py”, line 81, in Connect
  File “build/bdist.linux-i686/egg/MySQLdb/connections.py”, line 188, in __init__
_mysql_exceptions.OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)”)

mysql启动是参数设定的比较多,我的mysql启动脚本:
/home/2hei/mysql/bin/mysqld –defaults-file=/home/2hei/mysql/etc/my.cnf –basedir=/home/2hei/mysql –datadir=/home/2hei/mysql/data –log-error=/home/2hei/mysql/data/2hei.net.err –pid-file=/home/2hei/mysql/data/mysqld.pid –socket=/tmp/mysql.sock –port=13306

#!/usr/bin/env python
# -*-coding:UTF-8-*
import MySQLdb
conn = MySQLdb.Connection(host=’localhost’,port=13306,user=’2hei’,passwd=’123456′,db=’pytest’)
cur = conn.cursor()
cur.execute(‘select * from test’)
row=cur.fetchall()
print row

一直提示我mysql.scok出错。看了MySQLdb的源码后发现可以设定的参数还有很多
————————————
class Connection(_mysql.connection):

    “””MySQL Database Connection Object”””

    default_cursor = cursors.Cursor
   
    def __init__(self, *args, **kwargs):
        “””

        Create a connection to the database. It is strongly recommended
        that you only use keyword parameters. Consult the MySQL C API
        documentation for more information.

        host
          string, host to connect
         
        user
          string, user to connect as

        passwd
          string, password to use

        db
          string, database to use

        port
          integer, TCP/IP port to connect to

        unix_socket
          string, location of unix_socket to use

        conv
          conversion dictionary, see MySQLdb.converters

        connect_timeout
          number of seconds to wait before the connection attempt
          fails.

        compress
          if set, compression is enabled

        named_pipe
          if set, a named pipe is used to connect (Windows only)

        init_command
          command which is run once the connection is created

        read_default_file
          file from which default client values are read

        read_default_group
          configuration group to use from the default file

        cursorclass
          class object, used to create cursors (keyword only)

        use_unicode
          If True, text-like columns are returned as unicode objects
          using the connection’s character set.  Otherwise, text-like
          columns are returned as strings.  columns are returned as
          normal strings. Unicode objects will always be encoded to
          the connection’s character set regardless of this setting.

        charset
          If supplied, the connection character set will be changed
          to this character set (MySQL-4.1 and newer). This implies
          use_unicode=True.

        sql_mode
          If supplied, the session SQL mode will be changed to this
          setting (MySQL-4.1 and newer). For more details and legal
          values, see the MySQL documentation.
         
        client_flag
          integer, flags to use or 0
          (see MySQL docs or constants/CLIENTS.py)

        ssl
          dictionary or mapping, contains SSL connection parameters;
          see the MySQL documentation for more details
          (mysql_ssl_set()).  If this is set, and the client does not
          support SSL, NotSupportedError will be raised.

        local_infile
          integer, non-zero enables LOAD LOCAL INFILE; zero disables
    ————————–

于是修改源码为:
conn = MySQLdb.Connection(host=’localhost’,port=13306,user=’2hei’,passwd=’123456′,db=’pytest’,unix_socket=’/tmp/mysql.sock’)
这样执行后可以得到正确的结果:
$python link_mysql.py
((1L, ‘ronaldo’, 23L), (2L, ‘figo’, 30L))

问题得以解决!

本文固定链接: https://www.2hei.net/2009/03/31/python_use_mysql/ | 2hei.net

该日志由 u2 于2009年03月31日发表在 python 分类下,
原创文章转载请注明: python use mysql | 2hei.net
关键字:

报歉!评论已关闭.