一 安装mysql
1.检查是否安装了mysql
如果有MySql 说明已经安装,如果没有则下载安装这里不做介绍了。
二、配置MySQL
1点击上图中的MySQL 开启mysql服务
2.进入终端 输入 mysql
如果出现 -bash: mysql: command not found 我们还需要将mysql加入系统环境变量
进入 /usr/local/mysql/bin
检查目录下是否有mysql
如果有 终端输入:open ~/.bash_profile
打开文件后添加 PATH=$PATH:/usr/local/mysql/bin 后保存退出
终端中 source ~/.bash_profile
再次输入 mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.49 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement
mysql>
二、使用 PyMySql
针对 myql 第三方框架很多,这里使用 PyMySql
下载地址:https://github.com/PyMySQL/PyMySQL
1.安装
pip install PyMySQL
安装完成后 impor pymysql 不是 PyMySql
debug 后
这时候你上网查解决办法,有效方法几乎找不到都是前篇一律的,其实这还是安装了两个python 版本的问题所致,这事还是用到wxPython的安装方法即可解决
python3.4 -m pip install PyMySQL
再次Debug一下
成功运行,下一步就可以使用。
三、使用 PyMySQL
mysql> show databases;
mysql> CREATEDATABASE pinXian;
这时候需要输入root账户密码验证,因为安装mysql的适合并没有设置密码(设了也忘记了密码);这时候执行以下操作
a.苹果->系统偏好设置->最下边点MySQL 在弹出页面中关闭mysql服务(点击stop mysql server)
b.登录管理员权限 sudo su输入密码
进入终端输入:cd/usr/local/mysql/bin/
回车后输入以下命令来禁止mysql验证功能
sudo ./mysqld_safe--skip-grant-table
回车后mysql会自动重启(偏好设置中mysql的状态会变成running)
c.输入命令 ./mysql
回车后,输入命令 FLUSH PRIVILEGES;
回车后,输入命令 SET PASSWORD FOR 'root'@'localhost' =PASSWORD('你的新密码');
接着 输入
mysql -uroot -p 输入设置后的密码
这时候在文件中
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='kangxg198811', db='pinXian', charset='utf8')
Debug 运行 没有报错,成功完成数据库连接任务!
http://blog.csdn.net/airjordon/article/details/52184983
https://downloads.mysql.com/archives/community/
https://www.zhihu.com/question/41202027