1. First implement the command line to run MySQLThis part of the internet has a big God said already very clear, directly quoted, Doseha. Reference: http://www.lihui.info/mac-pydev-mysqldb/
Install MYSQLDB on Mac OS, have a lot of problems, summarize:
First, if the use of sudo easy_install mysql-python command installation, basically will not succeed, for two reasons: one is not found Mysql_config location, two is now basically installed 64-bit system and 64-bit Python, Conflicts with the default 32-bit installation package.
So you can only download http://sourceforge.net/projects/mysql-python/files/, I downloaded the mysql-python-1.2.4b4.tar.gz.
After decompression, modify the site.cfg, which comments a line:
- Mysql_config =/usr/local/mysql/bin/mysql_config
Remove the comment and change the path to the location where you installed MySQL.
The command line then enters the extracted mysql-python-1.2.4b4 directory and executes the sudo python setup.py install for installation.
The installation process encountered a lot of problems, and then found on the Internet two solutions. However, there is still a problem with this installation, because I use the Eclipse+pydev development environment and the environment is still not able to use MYSQLDB. But with these two solutions, you can import mysqldb at the command line, so also write down to see if there is a more perfect solution in the future.
The first issue is the installation process tip: Library not Loaded:libmysqlclient.18.dylib
Depending on http://blog.sina.com.cn/s/blog_68f3bc280100supn.html, add export dyld_library_path= "$DYLD to the environment variable _library_path:/ usr/local/mysql/lib/"or on a soft link, execute it on the command line:
- sudo ln-s/usr/local/mysql/lib/libmysqlclient.18.dylib/usr/lib/libmysqlclient.18.dylib
The second issue is the installation process tip: no suitable image found. Did Find:/library/python/2.7/site-packages/mysql_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so:mach-o, But wrong architecture
According to http://www.liuhuadong.com/archives/1628, this can be resolved:
Edit or add hidden files under/users/your username/directory. Bash_profile, add environment variables:
- path= "/usr/local/mysql/bin:${path}"
- Export PATH
- Export dyld_library_path=/usr/local/mysql/lib/
- Export Versioner_python_prefer_64_bit=yes
- Export Versioner_python_prefer_32_bit=yes
Then on the command line into the extracted mysql-python-1.2.4b4 directory, enter:
-
-
Recompile and perform the installation.
This will not be a problem after you execute import mysqldb at the command line
Configuration in 2.pydev The prerequisite is to complete the above steps First of all, to open the Preference-pydev-interpreter-python, click AutoConfig tick the package with MySQL, re-import. this time add import mysqldb Although not error, but once run, will be error:
Library not loaded:libmysqlclient.18.dylib referenced from:/library/python/2.7/site-packages/mysql_ Python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so
The solution is executed in the shell (note the path):
sudo install_name_tool-change Libmysqlclient.18.dylib/usr/local/mysql/lib/libmysqlclient.18.dylib/library/python /2.7/site-packages/mysql_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so
3. Testingin the shell, enter the following code to test if the link is successful.
Import Mysqldbtry: conn=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' test ', port=3306) cur=conn.cursor () cur.execute (' select * from MyTable ') Results=cur.fetchall () for R in results: Print R cur.close () conn.close () except mysqldb.error,e: print "Mysql Error%d:%s"% (E.args[0], E.ARGS[1])
output results, database query success: