Ununtu 15.04 安装MySql(Django连接Mysql)
本文介绍Ubuntu 15.04下安装MySQL
- ubuntu 15.04安装mysql
- django项目连接mysql
一.安装数据库
1.sudo apt-get install mysql-server
2.apt-get install client
3.sudo apt-get install libmysqlclient-dev
安装过程中会提示输入用户密码,输入即可。
sudo netstat -tap | grep mysql
检查mysql是否安装成功,如下显示就是安装成功了。
mingwei@mingwei:~$ sudo netstat -tap | grep mysql
[sudo] password for mingwei:
tcp 0 0 localhost:mysql *:* LISTEN 23411/mysqld
mingwei@mingwei:~$
4.mysql -u root -p
进入数据库, -u是用户名,这里是root用户,-p是密码
5.show databases;
查看所有数据库 结尾出有 “;”
6.use databasename
使用制定的库文件 databasename是数据库名
7.show tables;
列出所有表 结尾出有 “;”
8.create database databasename
创建一个库,databasename是库名
9.create database databasename default character set utf8 collate utf8_general_ci;
CREATE DATABASE databasename CHARACTER SET utf8; (推荐用这条命令)
创建一个库字符集编码为utf-8,databasename是库名
10. mysqldump -uroot -proot -d mfor > mfor.sql
将库导成.sql文件 -uroot 用户名 -proot密码 mfor 库的名字
安装PyMysql 库
在相应位置加入
10.drop database databasename;
删除数据库,databasename是库名
11.use databasename;
show tables;
use使用某一个库,show tables为展示这个库里面所有的表,databasename是库名。
12mac mysql error You must reset your password using ALTER USER statement before executing this statement. (mac下mysql错误)
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个
step 1: SET PASSWORD = PASSWORD('your new password');
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
step 3: flush privileges;
完成以上三步退出再登,使用新设置的密码就行了,以上除了红色的自己修改成新密码外,其他原样输入即可
mac 登录mysql
http://jingyan.baidu.com/article/48a42057e2b2b9a9242504a2.html
创建好之后测试看库有没有,第二部分我们使用django来连接这个库试试。
二.连接数据库
接下来我们在web项目中测试连接mysql
1.在django项目的app下的的models.py下编写项目的model,然后django自身的ORM会读取这些model,并在数据库中创建相应的表,字段。
我们写的例子如下:
from django.db import models # Create your models here.
class blogPost(models.Model):
blog_title=models.CharField(max_length=150)
blog_body=models.TextField()
blog_timestamp=models.DateTimeField()
2.在web项目下的settings.py下面配置mysql数据库的连接参数
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #默认
'NAME': 'mysite', #mysql的库名称
'USER': 'root', #用户名
'PASSWORD': '', #密码
'HOST': '127.0.0.1', #默认
'PORT': '', #默认
}
}
3.测试连接数据库
Python manage.py check
如果是这样:数据库连接失败了,查看结尾的提示,No module named MySqldb,
mingwei@mingwei:~/mysite$ python manage.py check
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/mingwei/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/home/mingwei/.local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
class Permission(models.Model):
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
value.contribute_to_class(cls, name)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/mingwei/.local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
缺少python连接数据的库,我们需要安装一下:
在虚拟环境下安装
pip install python-mysqldb
有可能在python3下提示安装pymysql==0.7.11
pymysql的用法与MySQLdb完全相同,只要记得在新建项目下面的_init__.py文件中添加如下代码就可以了~:
import pymysql
pymysql.install_as_MySQLdb()
安装完之后再测试,如果不报错就说明安装成功了。
4.根据我我们编写的model来生成数据库
python manage.py syncdb
mingwei@mingwei:~/mysite$ python manage.py syncdb
/home/mingwei/.local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning) Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'mingwei'):
Email address:
Password:
Password (again):
Error: Blank passwords aren't allowed.
Password:
Password (again):
Superuser created successfully.
生成了一大堆的东西,并且下面有successfully我都以为成功了,然后跑到mysql下去看,看到的是这样的,
mysite的库确实生成了,但是表并没有生成:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysite |
| mysql |
| performance_schema |
use mysite 进入库下面:
show tables;列出所有表,发现只有一些用户表:奇了怪了,说好的orm自动生成呢?
mysql> show tables;
+----------------------------+
| Tables_in_mysite |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
然后发现了是这个原因导致的,删掉你app项目下的 migrations文件夹,重新python manage.py syncdb, 哇。。。数据库奇迹般的生成了,,,,,如下表:bolg_bolgpost
mysql> show tables;
+----------------------------+
| Tables_in_mysite |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| blog_blogpost |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
11 rows in set (0.00 sec)
Ununtu 15.04 安装MySql(Django连接Mysql)的更多相关文章
- Ubuntu 15.04 安装配置Apache和mysql的方法
前 言 上篇海面特效的博文结尾提到了SideFX开发的HQueue分布式集群,配置HQueue首先需要安装mysql,所以今天先分享一下如何在Ubuntu系统中安装mysql和Apache: Ubun ...
- [技术博客]django连接mysql数据库的方法及部分问题的解决方法
配置机器介绍 操作系统:Ubuntu 18.04.2 LTS 64位 python版本:Python 3.6.7 Django版本:Django 2.2 MySql版本:5.7.26 数据库选择 我们 ...
- Django 连接mysql数据库中文乱码
Django 连接mysql数据库中文乱码 2018年08月25日 20:55:15 可乐乐乐乐乐 阅读数:566 版本:CentOS6.8 python3.6.4 django1.8.2 数据库 ...
- python测试开发django-10.django连接mysql
前言 Django 对各种数据库提供了很好的支持,包括:PostgreSQL.MySQL.SQLite.Oracle.本篇以mysql为例简单介绍django连接mysql进行数据操作 Django连 ...
- Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?'
Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?' 一.原因 因为Python版本问题,MySQLdb ...
- Python3之Django连接mysql数据库
一.mysql服务器 系统版本:CentOS6.8 IP :10.0.0.51 版本 1.授权root远程登录 grant all on *.* to 'root'@'%' identifide by ...
- Django 连接 MySQL 数据库及常见报错解决
目录 Django 连接 MySQL数据库及常见报错解决 终端或者数据库管理工具连接 MySQL ,并新建项目所需数据库 安装访问 MySQL 的 Python 模块 Django 相关配置 可能会遇 ...
- django连接mysql数据库以及建表操作
django连接mysql数据库需要在project同名的目录下面的__init__.py里面加入下面的东西 import pymysql pymysql.install_as_MySQLdb() 找 ...
- django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x97\\xAE\\xE9\\xA2\\x98' for column 'name' at row 5")
django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x ...
随机推荐
- hdu 1024 dp滚动数组
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...
- 【itercast OSI 七层网络模型 学习笔记】Layer 1 物理层
NIC:网卡(基本上是一层功能) 传输介质:以太网,分有线和无线 开始以太网只有10Mbps的吞吐量,使用的是带有冲突检测的载波侦听多路访问(CSMA/CD,Carrier Sense Multipl ...
- <The Art of Readable Code> 笔记二 (上)
第2章 封装信息到名字 (Packing information into names) 2.1 use specific words GetPage() 不如 FetchPage() 和 Dow ...
- Laravel 教程 - Web 开发实战入门 ( Laravel 5.5 )购买链接
Laravel 教程 - Web 开发实战入门 ( Laravel 5.5 )购买链接: 推荐给你高品质的实战课程 https://laravel-china.org/courses?rf=158 ...
- ant design pro (九)引入外部模块
一.概述 原文地址:https://pro.ant.design/docs/import-cn 除了 antd 组件以及脚手架内置的业务组件,有时我们还需要引入其他外部模块,这里以引入富文本组件 re ...
- javascript forEach方法与jQuery each区别
1.forEach方法 语法: array.forEach(function(currentValue, index, arr), thisValue) 参数: 示例: <!DOCTYPE ht ...
- js 给json添加新的字段,或者添加一组数据,在JS数组指定位置删除、插入、替换元素
JS定义了一个json数据var test={name:"name",age:"12"};需要给test再添加一个字段,需要什么办法,可以让test的值为{na ...
- 记一次CurrentDirectory导致的问题
现在项目里需要实现一个功能如下: A.exe把B.exe复制到临时目录,然后A.exe退出,B.exe负责把A.exe所在的整个目录删除. 实现: A.exe用CreateProcess创建B.exe ...
- Servlet 异常处理
当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类型相匹配的配置.您必须在 web.xml 中使用 error-pa ...
- SecureCRT 常用配置
1.SecureFx 中文乱码,应设置成utf-8编码了,依旧乱码 在 C:\Users\root\AppData\Roaming\VanDyke\Config\Sessions 下找到对应的sess ...