系统环境: Ubuntu 16.0.4
Python版本: 2.7
在进行此操作前请确保你有 Python
和 pip
.虽然 Linux 系统自带了 python.
安装 MySQL-python
命令行安装:
sudo pip install MySQL
当然你也可以去该网站下载后安装: ![MySQL-Python](https://pypi.python.org/pypi/MySQL-python/)
下载MySQL-python-1.2.5.zip 文件之后直接解压。进入MySQL-python-1.2.5目录:
python setup.py install
错误
xcy@ultravires:~$ sudo easy_install MySQLdb
Searching for MySQLdb
Reading https://pypi.python.org/simple/MySQLdb/
No local packages or download links found for MySQLdb
error: Could not find suitable distribution for Requirement.parse('MySQLdb')
xcy@ultravires:~$ sudo pip install MySQL
The directory '/home/xcy/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/xcy/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting MySQL
Downloading mysql-0.0.1.tar.gz
Collecting MySQL-python (from MySQL)
Downloading MySQL-python-1.2.5.zip (108kB)
100% |████████████████████████████████| 112kB 28kB/s
Complete output from command python setup.py egg_info:
sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-63GYXb/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "/tmp/pip-build-63GYXb/MySQL-python/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/tmp/pip-build-63GYXb/MySQL-python/setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-63GYXb/MySQL-python/
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
解决
依次执行下面的命令:
sudo apt-get install libmysqlclient-dev
sudo updatedb
locate mysql_config
sudo pip install mysql
执行结果如下:
xcy@ultravires:~$ sudo apt-get install libmysqlclient-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
下列【新】软件包将被安装:
libmysqlclient-dev
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 2 个软件包未被升级。
需要下载 1,159 kB 的归档。
解压缩后会消耗 7,072 kB 的额外空间。
获取:1 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libmysqlclient-dev amd64 5.7.19-0ubuntu0.16.04.1 [1,159 kB]
已下载 1,159 kB,耗时 0秒 (2,137 kB/s)
正在选中未选择的软件包 libmysqlclient-dev。
(正在读取数据库 ... 系统当前共安装有 302797 个文件和目录。)
正准备解包 .../libmysqlclient-dev_5.7.19-0ubuntu0.16.04.1_amd64.deb ...
正在解包 libmysqlclient-dev (5.7.19-0ubuntu0.16.04.1) ...
正在处理用于 man-db (2.7.5-1) 的触发器 ...
正在设置 libmysqlclient-dev (5.7.19-0ubuntu0.16.04.1) ...
xcy@ultravires:~$ sudo updatedb
xcy@ultravires:~$ locate mysql_config
/usr/bin/mysql_config
/usr/bin/mysql_config_editor
/usr/local/MATLAB/R2015b/help/database/ug/dexplore_create_mysql_config.jpg
/usr/share/man/man1/mysql_config.1.gz
/usr/share/man/man1/mysql_config_editor.1.gz
xcy@ultravires:~$ sudo pip install MySQL
The directory '/home/xcy/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/xcy/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting MySQL
Downloading mysql-0.0.1.tar.gz
Collecting MySQL-python (from MySQL)
Downloading MySQL-python-1.2.5.zip (108kB)
100% |████████████████████████████████| 112kB 17kB/s
Installing collected packages: MySQL-python, MySQL
Running setup.py install for MySQL-python ... done
Running setup.py install for MySQL ... done
Successfully installed MySQL-0.0.1 MySQL-python-1.2.5
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
xcy@ultravires:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
在Python的命令交互模式下输入
import MySQLdb
如果没有报错,说明安装成功。