linux django 安装mysql_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是库名

创建好之后测试看库有没有,第二部分我们使用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

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

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

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)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值