安装
1 2 3
| sudo apt-get install mysql-server sudo apt-get install libmysqlclient-dev sudo pip install MySQL-python
|
绘图
使用mysql workbench自动绘制ER图
database -> Reverse Engineer
File -> export as png
sqlalchemy
1 2 3
| ./manage.py shell db.create_all() db.drop_all()
|
生成测试数据
需要使用 ForgeryPy
1 2
| forgery_py.internet.email_address() forgery_py.internet.user_name(True)
|
驱动安装
遇到例如ImportError: No module named MySQLdb
的错误
1 2 3 4
| pip install pymysql sudo apt-get install python-pymysql 并修改config SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'
|
http://stackoverflow.com/questions/22252397/importerror-no-module-named-mysqldb
utf8编码问题
代码部分
出现
‘ascii’ codec can’t decode byte
添加
1 2 3 4 5
| import sys default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding)
|
mysql配置部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # 查看 show variables like # 关闭mysql服务 service mysql stop # 修改conifg文件 vim /etc/mysql/my.cnf # 打开后,在文件内的[mysqld]下增加两行设置 character_set_server=utf8 init_connect= default-character-set = utf8 # 重启mysql sevice mysql start # 查看 show variable like # 修改字符集 alter database devdb character set utf8;
|
把connstr修改为connstr = ‘mysql://uid:pwd@localhost/mydb?charset=utf8’
mysql 用户权限管理
创建用户
1 2
| CREATE USER 'test'@'%' IDENTIFIED BY 'test'; CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
|
%表示任意IP可访问,localhost只能本地访问。
授权用户
授权用户某一数据库的所有权限,并刷新权限表
1 2
| grant all privileges on devdb.* to test@'%' identified by 'test'; flush privileges;
|
授权用户某一数据库的部分权限
1 2
| grant select,update on testDB.* to test@localhost identified by 'test'; flush privileges;
|
授权用户所有数据库的部分权限
1 2
| grant select,update on *.* to test@localhost identified by 'test'; flush privileges;
|
mysql 常见操作
1 2 3 4 5 6 7
| show databases; use database_name; show tables; describe table_name; drop database database_name; drop table table_name; create database devdb;
|
关于存储引擎
1 2
| show engines; show variables like '%storage_engine%';
|
参考资料
http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#auto-increment-behavior
SQLAlchemy MySQL数据库乱码解决
https://www.linuxzen.com/sqlalchemy-mysqlshu-ju-ku-luan-ma-jie-jue.html
sqlalchemy中文问题解决方案
http://firefish.blog.51cto.com/298258/112794/
MySQL编码设置方法 MySQL编码为utf8设置方法
http://www.cr173.com/html/50452_1.html
SQLAlchemy 使用经验
https://www.keakon.net/2012/12/03/SQLAlchemy%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C
SQLAlchemy 简单笔记
http://www.jianshu.com/p/e6bba189fcbd
sqlalchemy的几种用法
http://my.oschina.net/freegeek/blog/222725
python {} string
“abc{}sadad”.format()