安装的时候,要注意linux自带的python安装。如果想自己安装的python生效。
就修改/etc/profile文件,将python安装的路径写在前面如下:
PATH="/usr/local/bin/:$PATH"
安装的时候,我是用默认安装,所以python2.7新版本在/usr/local/bin目录下,写在前面然后保存。source /etc/profile使修改生效。
然后再运行python -V,就能看到新版本信息如下:
[root@master ~]# python -V
Python 2.7.3
[root@master ~]#
第二问题就是安装Mysql的时候,setuptools工具是需要的。
安装步骤如下:
wget -O setuptools-0.6c8.tar.gz http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c8.tar.gz
tar -xzvf setuptools-0.6c8.tar.gz
cd setuptools-0.6c8
python setup.py build
sudo python setup.py install
继续安装MySQL-python-1.2.3,
tar -zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py build
python setup.py install
如果运行python setup.py build 命令报错信息如下:
[root@master MySQL-python-1.2.3]# python setup.py build
sh: mysql_config: command not found
Traceback (most recent call last):
File "setup.py", line 15, in ?
metadata, options = get_config()
File "/root/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/root/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
说明有个文件属性没打开,修改site.cfg,将mysql_config这个属性打开,如下:
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)
embedded = False
threadsafe = True
static = False
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /usr/local/mysql/bin/mysql_config
# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0
注意以上内容红色字体部分,与你安装的mysql目录有关,自己去找自己安装的mysql目录即可。
完成有,进入到python环境里。
python
>>> import MySQLdb
>>>
如果没有错误,就正常了。
如果出现下面的错误信息如下:
libmysqlclient.so.18: cannot open shared object file: No such file or directory
错误的原因是未能引入libmysqlclient.so.18库,
查找库文件存在,于是建立链接:
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 libmysqlclient.so.18.0.0
如下面内容:
[root@master lib]# pwd
/usr/local/mysql/lib
[root@master lib]# ls -lh
total 12M
-rw-r--r-- 1 mysql mysql 7.8M Feb 14 09:54 libmysqlclient.a
lrwxrwxrwx 1 mysql mysql 16 Feb 14 09:56 libmysqlclient_r.a -> libmysqlclient.a
lrwxrwxrwx 1 mysql mysql 17 Feb 14 09:56 libmysqlclient_r.so -> libmysqlclient.so
lrwxrwxrwx 1 mysql mysql 17 Feb 14 09:56 libmysqlclient_r.so.18 -> libmysqlclient.so
lrwxrwxrwx 1 mysql mysql 17 Feb 14 09:56 libmysqlclient_r.so.18.0.0 -> libmysqlclient.so
lrwxrwxrwx 1 mysql mysql 20 Feb 14 09:56 libmysqlclient.so -> libmysqlclient.so.18
lrwxrwxrwx 1 mysql mysql 24 Feb 14 09:56 libmysqlclient.so.18 -> libmysqlclient.so.18.0.0
-rwxr-xr-x 1 mysql mysql 4.3M Feb 14 09:54 libmysqlclient.so.18.0.0
-rw-r--r-- 1 mysql mysql 9.3K Feb 14 09:53 libmysqlservices.a
drwxr-xr-x 3 mysql mysql 4.0K Feb 14 09:56 plugin
注意红色字体部分。
如果还有问题。
就修改/etc/profile文件里,添加如下内容:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib:/usr/local/lib
注意粗体红色字体部分,将lib目录加入到LD_LIBRARY_PATH环境变量中,保存并运行source /etc/profile使修改生效。
这样就不有上面的错误了。
后面还有一种错误类型如下:
报错 /usr/bin/ld: cannot find -lmysqlclient_r
切换到root用户,执行
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf #请主意这个/etc/ld.so.conf,这个是ld的默认配置文件,视系统不同而不同。 /usr/local/mysql/lib/mysql与mysql安装目录相关。
强制刷新ld的配置文件。使用:ldconfig