红联Linux门户
Linux帮助

Ununtu 15.04安装MySql(Django连接Mysql)

发布时间:2015-09-05 09:34:52来源:linux网站作者:顾明伟

本文介绍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是库名

创建好之后测试看库有没有,第二部分我们使用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', 
'USER': 'root', 
'PASSWORD': '123456', 
'HOST': '127.0.0.1', 
'PORT': '3306', 


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连接数据的库,我们需要安装一下:

sudo apt-get install python-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)


Ubuntu15.04下MySQL5.6安装过程:http://www.linuxdiyf.com/linux/13250.html

Ubuntu 14.10下编译安装MySQL 5.6.23:http://www.linuxdiyf.com/linux/12221.html

Ubuntu 15.04安装配置Apache和mysql的方法:http://www.linuxdiyf.com/linux/13074.html

Ubuntu15.X和CentOS7之后-MySQL源码编译安装:http://www.linuxdiyf.com/linux/13144.html

ubuntu15.04 qt5.4.2连接mysql:http://www.linuxdiyf.com/linux/13054.html