lanbos'blog

django 初学(三)mysql

mysql安装

虽然网上推荐去官网下载安装包进行安装,但是我还是选择了用brew直接安装,安装前一定要将之前安装的mysql卸载完整,附卸载mysql完整命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ps -ax | grep mysql stop and kill any MySQL processes
brew remove mysql
brew cleanup
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/mysql*
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
#edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*
#edit ~/.bash_profile and remove any aliases for mysql or mysqlAdmin

之后重启mac,打开终端进行安装(注意我安装的是mysql5.6版本的,因为之前安装5.7版本各种报错尝试了各种办法也无法解决):

1
2
brew install  homebrew/versions/mysql56
mysql --version

输入查看版本号命令之后显示版本号表示成功,之后按照brew安装mysql的提示输入(注意mac下的文件操作权限问题,很尴尬可以对/usr/local/mysql整个文件夹及子文件夹设置读写权限):

1
2
3
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mysql.server start

数据库启动。

安装数据库驱动

使用pip安装驱动:

1
2
3
4
5
6
#官方驱动,没有安装成功
pip install mysql-connector-python --allow-external mysql-connector-python
# 支持到python3.4 我用的python 3.5
pip install MySQLdb
# 最终选择
pip install PyMysql

这里有个坑是配置好setting.py后,启动服务会报错找不到Module ‘MySQLdb’,看python3+Django+MySQL+pymysql后,在站点的__init__.py文件中添加:

1
2
import pymysql
pymysql.install_as_MySQLdb()

成功。

同步数据库命令

1
2
python manager.py makemigrations
python manager.py migrate