作者:十字彩虹00 | 来源:互联网 | 2023-07-22 21:55
Linux非root用户下安装python及MySQLdb记录:安装所需软件包:Python-2.6.tar.bz2、MySQL-python-1.2.3c1.tar、setuptools-0.6c
Linux非root用户下安装python及MySQLdb记录:
安装所需软件包: Python-2.6.tar.bz2、MySQL-python-1.2.3c1.tar、setuptools-0.6c11.tar.gz。
安装前:
1. 定义python的安装目录:
PYTHON_DIR = /home/$USER/local/python
2. 定义安装目录:
INSTALL_DIR = /home/$USER/install
先查看本机python版本,如果已安装相应版本则跳过第1步;
python
Python 2.6 (r26:66714, Jul 21 2010, 11:47:32)
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Ctrl + D退出第1步, 安装python:
tar -C $INSTALL_DIR -xjvf Python-2.6.tar.bz2
cd $INSTALL_DIR/Python-2.6
./configure --prefix = $PYTHON_DIR
make && make install
3. 安装MySQLdb:
tar -C $INSTALL_DIR -xzvf MySQL-python-1.2.3c1.tar
cd $INSTALL_DIR/MySQL-python-1.2.3c1
4. 修改配置项
mysql_cOnfig=your_mysql_dir/mysql-bin/bin/mysql_config
vi site.cfg
$PYTHON_DIR/bin/python setup.py build
$PYTHON_DIR/bin/python setup.py install --prefix = $PYTHON_DIR
这一步可能会提示ImportError: No module named setuptools,则需要安装setuptools模块。
5. 安装setuptool:
tar -C $INSTALL_DIR -xzvf setuptools-0.6c11.tar.gz
cd $INSTALL_DIR/setuptools-0.6c1
$PYTHON_DIR/bin/python setup.py build
$PYTHON_DIR/bin/python setup.py install
6. 回到MySQL-python-1.2.3c1目录,执行安装MySQLdb最后一步:
cd $INSTALL_DIR/MySQL-python-1.2.3c1
$PYTHON_DIR/bin/python setup.py install --prefix = $PYTHON_DIR
这一步可能会提示如下错误:
/usr/bin/ld: cannot find -lmysqlclient_r
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
7. 修改配置项,
embedded = False
threadsafe = False
static = False
vi site.cfg
再执行:
$PYTHON_DIR/bin/python setup.py install --prefix = $PYTHON_DIR
安装完成测试:
python
Import MySQLdb
如果报错:
ImportError: libmysqlclient.so: cannot open shared object file: No such file or directory
cd ~ && vi .bash_profile
在最后增加
export LD_LIBRARY_PATH=/home/$USER/local/mysql/lib/mysql:$LD_LIBRARY_PATH
重启session,再测试正常安装完成!
python: https://www.python.org/downloads/source/
setuptool:http://pypi.python.org/pypi/setuptools
MySQL-python: http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download
from: http://os.51cto.com/art/201104/256066.htm