Django Note--mysql installation

Source: Internet
Author: User
Tags mysql version

Recently I was learning about Django, and I was going to play with the background of the web, because I was curious but didn't have much contact. Django for me is a brand new content, thinking to want is also brand-new, perhaps not written very clear, so we will make it easy to see ~

This note (In fact all my notes) is not explained in detail. So if there are people can not understand the place, welcome to my original blog under the message, there is time I am willing to come here to discuss the problem with you. (Of course, can not be simple to Baidu to the problem-.-)

The textbook I chose is "the Django Book 2.0", this section is my own inserted database installation steps, for students who do not know the database installation.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

0. Description

In order to use the database in Django, it needs to be installed and set up, in "The Django Book 2.0", the default is that you have installed the database you want to use, but in fact, not everyone has learned how to install, and even very troubled, And it's hard to find a tutorial that suits you (I'm-.-).

Let me show you the preparation required to use the database in Django.

The first is the installation. The database I'm using is MySQL, so I need to have MySQL installed on my machine and install its servers and clients as well. Then, install the Mysql-python library so that you can use MySQL in Python code.

Then it is set up to have a user and database in MySQL for you to use (create one if not already available). Then, you need to set all the database settings in your Django project's settings.py to locate the database you want to use and the users to use it.

Finally, as long as the database server is running, you can use the database in Django according to your previous settings.

  Again, the following commands are to be entered in the terminal, no longer repeat.

1. Install MySQL

My MySQL version is 5.6.24. (All my work environment configuration information, such as the operating system, has already been mentioned in the Django Notes-introductory introduction, not described here)

If you want to see if there's anything in your machine, just type MySQL in the command line, and there's a response.

  If not, it does not matter, the following installation of the server will be installed automatically.

2. Install MySQL server and client

sudo apt-get install Mysql-server

sudo apt-get install mysql-client

If you do not have MySQL in your previous machine, you will be automatically installed when you install the server, and will let you set the root password in MySQL, which is often used in future MySQL use.

As for the root user, basically it feels like the root user of Linux, and here's a quick explanation.

Use MySQL to log in with a user, each user has their own permissions, and root is the most privileged users, with any permissions, can manage all other users. In fact, when we use MySQL, we often have another normal user in addition to the root user. When used normally, use that ordinary user, when the authority is insufficient, then root. In this way, you can avoid shaking your hands to delete things that should not be removed ~

3, Installation Mysql-python

sudo pip install Mysql-python

Note that the case is not written wrong.

4. Create users and databases in MySQL

Now that your MySQL is installed, you need to create a user and a database in MySQL for Django to use.

I'll teach you. Log in to the database with root:

Enter Mysql-u root-p and enter your MySQL root password.

See mysql> that you log in, Exit command is Exit,mysql will and you say "Bye".

Then the new User:

1. Log in with Root

2. Create User: INSERT into Mysql.user (Host, user, Password) values ("localhost", "QiQi", Password ("Nicai"));

3. Refresh Permissions table: Flush privileges;

This creates a user named QiQi, whose password is Nicai. The type of this user is localhost, which is the user logged on locally. Not much to say, learning Django is enough for this user.

Finally create a database:

1. Log in with Root

2. Creating a database: Create databases Django;

This creates a database named Django.

It's not over yet, and finally give QiQi permission to use the Django database:

1. Log in with Root

2. Grant All rights: Grant all privileges the django.* to [e-mail protected] identified by ' Nicai ';

At this point, you can use the QiQi user to log in to MySQL directly and manipulate the database Django, and the following settings are required for use in Django:

5. Set in Django

Open the settings.py in your Django project, find the DATABASES property, and set it up like this:

1 #Database2 #https://docs.djangoproject.com/en/1.8/ref/settings/#databases3 4DATABASES = {5     'default': {6         'ENGINE':'Django.db.backends.mysql',7         'NAME':'Django',8         'USER':'QiQi',9         'PASSWORD':' Nicai',Ten         'HOST':'127.0.0.1', One         'PORT':'3306', A     } -}

Which, because I am operating on this machine, so host is this; As for the port, I found it on the Internet, it should be the default port of MySQL.

Here's a note, do you see the URL at the beginning of the code? This is the documentation link for Django's description of this parameter. If you encounter problems, you must use the documentation!

6. Running the database server

Once you've installed it, the MySQL server will automatically run every time it is powered on. So this can be omitted.

7. Try to use

In the root directory of the project you just changed settings.py, run: Python manage.py shell

If you can enter normally, it means that Django can already navigate through the settings in settings.py to the users and databases you want to use in MySQL.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

At this point, we should be able to continue the Django study.

I might write about MySQL if I have a chance to use it later or if you need it. Now, go back to Django's study.

Django Note--mysql installation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.