MySQL的安装和与Python的连接(Ubuntu)

来源:互联网 发布:微信公众号js sdk 编辑:程序博客网 时间:2024/06/07 19:33

在Ubuntu使用Python,并让Python与MySQL连接进行相应的开发。

首先就是的MySQL的安装了,使用Ubuntu安装MySQL是十分的方便的,使用

sudo apt-get install mysql-serversudo apt-get install mysql-client

进行MySQL的安装,在安装的过程中会提醒设置用户(root)的密码,记得填写,免去后续的麻烦。

下一步的就是的安装Python与MySQL相连接的工具MySQLdb,网站(https://pypi.python.org/pypi/MySQL-python/1.2.5),下载的是一个压缩的文件,解压找到setup.py文件。

leeguo@leeguo-PC:~/Downloads$ cd MySQL-python-1.2.5/leeguo@leeguo-PC:~/Downloads/MySQL-python-1.2.5$ lsbuild    MANIFEST.in            PKG-INFO          setup_posix.pydist     metadata.cfg           pymemcompat.h     setup_posix.pycdoc      _mysql.c               README.md         setup.pyGPL-2.0  MySQLdb                setup.cfg         setup_windows.pyHISTORY  _mysql_exceptions.py   setup_common.py   site.cfgINSTALL  MySQL_python.egg-info  setup_common.pyc  tests

如果提示出现的这样的错误,说明有些东西没有安装,下面有解决的办法。若没有这样的问题请无视

###########分割线begin
leeguo@leeguo-PC:~/Downloads/MySQL-python-1.2.5$ python setup.py installsh: 1: /usr/bin/mysql_config: not foundTraceback (most recent call last):  File "setup.py", line 17, in <module>    metadata, options = get_config()  File "/home/leeguo/Downloads/MySQL-python-1.2.5/setup_posix.py", line 44, in get_config    libs = mysql_config("libs_r")  File "/home/leeguo/Downloads/MySQL-python-1.2.5/setup_posix.py", line 25, in mysql_config    raise EnvironmentError("%s not found" % (mysql_config.path,))EnvironmentError: /usr/bin/mysql_config not found

安装时出现上面的提示,说明没有安装libmysqlclient-dev,那就安装:

leeguo@leeguo-PC:~/Downloads/MySQL-python-1.2.5$ sudo apt-get install libmysqlclient-devReading package lists... DoneBuilding dependency tree       Reading state information... DoneThe following additional packages will be installed:  libmysqlclient20The following NEW packages will be installed:  libmysqlclient-dev libmysqlclient200 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.Need to get 0 B/1,968 kB of archives.After this operation, 11.5 MB of additional disk space will be used.Do you want to continue? [Y/n] YSelecting previously unselected package libmysqlclient20:amd64.(Reading database ... 243728 files and directories currently installed.)Preparing to unpack .../libmysqlclient20_5.7.19-0ubuntu0.16.04.1_amd64.deb ...Unpacking libmysqlclient20:amd64 (5.7.19-0ubuntu0.16.04.1) ...Selecting previously unselected package libmysqlclient-dev.Preparing to unpack .../libmysqlclient-dev_5.7.19-0ubuntu0.16.04.1_amd64.deb ...Unpacking libmysqlclient-dev (5.7.19-0ubuntu0.16.04.1) ...Processing triggers for libc-bin (2.23-0ubuntu9) ...Processing triggers for man-db (2.7.5-1) ...Setting up libmysqlclient20:amd64 (5.7.19-0ubuntu0.16.04.1) ...Setting up libmysqlclient-dev (5.7.19-0ubuntu0.16.04.1) ...Processing triggers for libc-bin (2.23-0ubuntu9) ...
###########分割线end

解决上面的问题安装MySQLdb了。

leeguo@leeguo-PC:~/Downloads/MySQL-python-1.2.5$ sudo python setup.py buildrunning buildrunning build_pycopying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdbrunning build_extleeguo@leeguo-PC:~/Downloads/MySQL-python-1.2.5$ sudo python setup.py installrunning installrunning bdist_eggrunning egg_infowriting MySQL_python.egg-info/PKG-INFOwriting top-level names to MySQL_python.egg-info/top_level.txtwriting dependency_links to MySQL_python.egg-info/dependency_links.txtreading manifest file 'MySQL_python.egg-info/SOURCES.txt'reading manifest template 'MANIFEST.in'writing manifest file 'MySQL_python.egg-info/SOURCES.txt'installing library code to build/bdist.linux-x86_64/eggrunning install_librunning build_pycopying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdbrunning build_extcreating build/bdist.linux-x86_64/eggcopying build/lib.linux-x86_64-2.7/_mysql_exceptions.py -> build/bdist.linux-x86_64/eggcreating build/bdist.linux-x86_64/egg/MySQLdbcopying build/lib.linux-x86_64-2.7/MySQLdb/release.py -> build/bdist.linux-x86_64/egg/MySQLdbcopying build/lib.linux-x86_64-2.7/MySQLdb/times.py -> build/bdist.linux-x86_64/egg/MySQLdbcreating build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/FLAG.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/ER.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/REFRESH.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/FIELD_TYPE.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/CLIENT.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/constants/CR.py -> build/bdist.linux-x86_64/egg/MySQLdb/constantscopying build/lib.linux-x86_64-2.7/MySQLdb/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdbcopying build/lib.linux-x86_64-2.7/MySQLdb/cursors.py -> build/bdist.linux-x86_64/egg/MySQLdbcopying build/lib.linux-x86_64-2.7/MySQLdb/converters.py -> build/bdist.linux-x86_64/egg/MySQLdbcopying build/lib.linux-x86_64-2.7/MySQLdb/connections.py -> build/bdist.linux-x86_64/egg/MySQLdbcopying build/lib.linux-x86_64-2.7/_mysql.so -> build/bdist.linux-x86_64/eggbyte-compiling build/bdist.linux-x86_64/egg/_mysql_exceptions.py to _mysql_exceptions.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/release.py to release.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/times.py to times.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FLAG.py to FLAG.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/__init__.py to __init__.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/ER.py to ER.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/REFRESH.py to REFRESH.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FIELD_TYPE.py to FIELD_TYPE.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CLIENT.py to CLIENT.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CR.py to CR.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/__init__.py to __init__.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/cursors.py to cursors.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/converters.py to converters.pycbyte-compiling build/bdist.linux-x86_64/egg/MySQLdb/connections.py to connections.pyccreating stub loader for _mysql.sobyte-compiling build/bdist.linux-x86_64/egg/_mysql.py to _mysql.pyccreating build/bdist.linux-x86_64/egg/EGG-INFOcopying MySQL_python.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFOcopying MySQL_python.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFOcopying MySQL_python.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFOcopying MySQL_python.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFOwriting build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txtzip_safe flag not set; analyzing archive contents...creating 'dist/MySQL_python-1.2.5-py2.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to itremoving 'build/bdist.linux-x86_64/egg' (and everything under it)Processing MySQL_python-1.2.5-py2.7-linux-x86_64.eggRemoving /usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.eggCopying MySQL_python-1.2.5-py2.7-linux-x86_64.egg to /usr/local/lib/python2.7/dist-packagesMySQL-python 1.2.5 is already the active version in easy-install.pthInstalled /usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.eggProcessing dependencies for MySQL-python==1.2.5Finished processing dependencies for MySQL-python==1.2.5

进行相应的测试,测试MySQLdb是否安装成功。

leeguo@leeguo-PC:~/Downloads/MySQL-python-1.2.5$ pythonPython 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import MySQLdb>>> 

像上面的这样,则说明MySQL安装成功了。

下面是一个关于Python和MySQL的小小程序,进行Python和MySQL的测试。

#!/usr/bin/python# -*- coding: UTF-8 -*-import MySQLdbdb = MySQLdb.connect(    host = "localhost",    user = "root",  #数据库的用户名    passwd = "001", #数据库的密码    db = "mysql",   #数据库    port = 3306)# 使用cursor()方法获取操作游标 cursor = db.cursor()# 使用execute方法执行SQL语句cursor.execute("SELECT VERSION()")# 使用 fetchone() 方法获取一条数据库。data = cursor.fetchone()print "Database version : %s " % data# 关闭数据库连接db.close()

运行程序,结果如下,说明环境搭建完成完成,可以进行下一步的工作。

leeguo@leeguo-PC:~/Nutstore/Python_code$ python mysql_test.py Database version : 5.7.19-0ubuntu0.16.04.1 
原创粉丝点击