学习Python on mac©️操作mysql

没有数据库支持的程序都是耍流氓

安装mysql驱动

在下面的网址下载mysqldb模块:
http://sourceforge.net/projects/mysql-python/
不用怀疑,2010年的版本;
在mac os x直接双击解压,命令行进入解压后的目录, 执行python setup.py build

python setup.py install

运行后发现没有安装mysql,这个时候编译python-mysql可能无法通过,抓紧下载mysql for mac
www.baidu.com上搜索mysql for mac,下载安装

安装方法,请参考
https://dev.mysql.com/downloads/mysql/
下载dmg版本,300M+

编译python setup.py build报错
sh: mysql_config: command not found Traceback (most recent call last): File "setup.py", line 18, in metadata, options = get_config() File "/Users/tuanwei/Downloads/MySQL-python-1.2.4b4/setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "/Users/tuanwei/Downloads/MySQL-python-1.2.4b4/setup_posix.py", line 25, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found SdeMacBook-Air:MySQL-python-1.2.4b4 tuanwei$ python setup.py build sh: mysql_config: command not found Traceback (most recent call last): File "setup.py", line 18, in metadata, options = get_config() File "/Users/tuanwei/Downloads/MySQL-python-1.2.4b4/setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "/Users/tuanwei/Downloads/MySQL-python-1.2.4b4/setup_posix.py", line 25, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found

这里需要修改python-mysql中的site.cfg中的关于mysql_config的配置,打开site.cfg.找到mysql_config,去掉注释,修改到/usr/local/mysql/bin(默认安装后的mysql,会在此目录下生成mysql_config文件)

修改后,编译通过,很快。
SdeMacBook-Air:MySQL-python-1.2.4b4 tuanwei$ python setup.py build running build running build_py creating build creating build/lib.macosx-10.12-x86_64-2.7 copying _mysql_exceptions.py -> build/lib.macosx-10.12-x86_64-2.7 creating build/lib.macosx-10.12-x86_64-2.7/MySQLdb copying MySQLdb/__init__.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb copying MySQLdb/converters.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb copying MySQLdb/connections.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb copying MySQLdb/cursors.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb copying MySQLdb/release.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb copying MySQLdb/times.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb creating build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/CR.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/ER.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.12-x86_64-2.7/MySQLdb/constants running build_ext building '_mysql' extension creating build/temp.macosx-10.12-x86_64-2.7 clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,4,'beta',4) -D__version__=1.2.4b4 -I/usr/local/mysql/include -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.12-x86_64-2.7/_mysql.o -fno-omit-frame-pointer -arch x86_64 _mysql.c:1563:10: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare] if (how < 0 || how >= sizeof(row_converters)) { ~~~ ^ ~ 1 warning generated. clang -bundle -undefined dynamic_lookup build/temp.macosx-10.12-x86_64-2.7/_mysql.o -L/usr/local/mysql/lib -L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/sqlite/lib -lmysqlclient -o build/lib.macosx-10.12-x86_64-2.7/_mysql.so -arch x86_64

安装mysql-python完成,修改环境变量

打开终端,在终端中使用vim打开“~/.bash_profile”,如果没有安装vim,那就显示隐藏文件用文本编辑器打开,具体操作这里就不复述了。在.bash_profile中添加以下内容:
PATH="/usr/local/mysql/bin:${PATH}" export PATH export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/ export VERSIONER_PYTHON_PREFER_64_BIT=no export VERSIONER_PYTHON_PREFER_32_BIT=yes

启动mysql

打开控制面板,找到mysql控制台,启动mysql

学习Python on mac©️操作mysql_第1张图片
Paste_Image.png

找到python操作mysql的样例,修改mysql的配置文件

学习Python on mac©️操作mysql_第2张图片
Paste_Image.png

下载mysql for mac的客户端

https://dev.mysql.com/downloads/workbench/

很郁闷,下载后登录不了,修改root密码
mac上安装mysql路径: /usr/local/mysql/

修改root密码

cd /usr/local/mysql/bin sudo su ./mysqld_safe --skip-grant-tables

重新打开一个终端
cd /usr/local/mysql/bin mysql flush privileges; set password for 'root'@'localhost'=password('fith2017');

退出后,kill掉进程
kill -9 mysqld_safe

添加mysql环境

打开一个终端
命令:vim ~/.bash_profile
最后一行添加 PATH=$PATH:/usr/local/mysql/bin,保存退出
命令source ~/.bash_profile

这里编译还有可能出现异常

_mysql.c:44:10: fatal error: 'my_config.h' file not found

这是因为XAMPP等,如果是IDE安装的话,没有编译环境,所以需要单独安装mysql-connector-c或者重新安装mysql

brew install mysql-connector-c

你可能感兴趣的:(学习Python on mac©️操作mysql)