Get the App
SLTechnology News&Howtos  ›  Database  › 

How to configure python Link MYSQL for python MySQLdb

Shulou Source: shulou.com Published: 2022-05-31 12:41:51 11月15日 Update

This article introduces you how to configure python MySQLdb python link MYSQL, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Download MySQL for Python

Wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

2. Installation

Yum install python-devel-2.7.5-48.el7.x86_64

Tar zxvf MySQL-python-1.2.3.tar.gz

$cd MySQL-python-1.2.3

Modify mysql_config.path = "mysql_config" in setup_posix.py to the corresponding path under your mysql software

Mysql_config.path = "/ home/mysql/soft/mysql5717/bin/mysql_config"

$python setup.py build

$python setup.py install

[root@node1 lib] # python testconn.py

Traceback (most recent call last):

File "testconn.py", line 3, in

Import MySQLdb

File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in

File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in

File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in _ _ bootstrap__

ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory

[root@node1 lib] # find /-name libmysqlclient

[root@node1 lib] # find /-name libmysqlclient.so.20

/ home/mysql/soft/mysql5717/lib/libmysqlclient.so.20

Make a soft connection to the / usr/lib64 directory (64 is the system)

Ln-s / home/mysql/soft/mysql5717/lib/libmysqlclient.so.20 / usr/lib64/libmysqlclient.so.20

There was still a mistake and I couldn't find socket.

[root@node1 duanfj] # python testconn.py

Traceback (most recent call last):

File "testconn.py", line 6, in

Conn=MySQLdb.connect (host= "localhost", user= "root", passwd= "root", db= "test", port=3306,charset= "utf8")

File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 81, in Connect

File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 187, in _ _ init__

_ mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket'/ tmp/mysql.sock' (2)")

This simple soft link is done.

Ln-s / tmp/my3306.sock / tmp/mysql.sock

[root@node1 MySQL-python-1.2.3] # python testconn.py

one

one

Row1

two

Row2

three

Row

four

Row4

[root@node1 MySQL-python-1.2.3] #

#

[root@node1 MySQL-python-1.2.3] # cat testconn.py

#-*-coding: utf-8-*-

# mysqldb

Import MySQLdb

# connection

Conn=MySQLdb.connect (host= "localhost", user= "root", passwd= "root", db= "test", port=3306,charset= "utf8")

Cursor = conn.cursor ()

# write

Sql = "insert into test (aformab) values (% s% s)"

Param = (4, "row4")

N = cursor.execute (sql,param)

Print n

# query

N = cursor.execute ("select * from test")

For row in cursor.fetchall ():

For r in row:

Print r

# Delete

# close

Conn.close ()

On how to configure python MySQLdb python link MYSQL to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Links configuration content more help good done interests great achievements guys buddies articles directories knowledge articles systems paths software or utf-8 Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Apple Xiaomi Shulou Tech Info