Environment: Ubuntu16.4
Tool: Python3.5
First, install MySQL database
Terminal command:
sudo apt-get install mysql-serversudo apt-get install mysql-clientsudo apt-get Install Libmysqlclient-dev
The process will let you enter the user name (the default root) and password, enter and press the direction to jump to the OK button, and then press ENTER
sudo netstat-tap | grep MySQL detects if the installation was successful, and when in listen state it means the installation was successful
Second, MySQL configuration
1. Terminal command: Mysql-u root-p, followed by the input password prompt, enter the password after entering the MySQL interface, then create a MYSQL1 database: Creating the databases mysql1 default Charset=utf8;
2. Open a new terminal, we create a new MySQL1 project, then switch to the MYSQL1 project, and then create a new blog app:
Terminal command:
Django-admin startproject mysql1cd mysql1django-admin Startapp Blog
3. Open mysql1/settings.py, Find Installed_apps, add the blog application just created in this list, find databases this dictionary, change the data inside, the following code:
Terminal command: VI mysql1/settings.py, press the I key to enter edit mode, ESC exit edit mode, press: WQ Save the file and exit. Do not habitually use CTRL + C, which in Ubuntu is the shortcut key to interrupt the program, press Ctrl+d Cancel
...... Installed_apps = [ ' django.contrib.admin ', ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.contrib.sessions ', ' django.contrib.messages ', ' django.contrib.staticfiles ', ' blog ', # Newly added apps] ... DATABASES = {' default ': { ' ENGINE ': ' Django.db.backends.mysql ', #用的哪种数据库 ' NAME ': ' Mysql1 ', # The name of the database (the following is the Arabic numeral 1) ' USER ': ' Root ', #用户名 ' PASSWORD ': ' ****** ', #密码 ' HOST ': ', #MySQL默认是localhost ' PORT ': ', #MySQL默认端口是3306 }} ......
4. Open blog/models.py, create a person class and class property in the file name, the code is as follows:
Terminal command: VI blog/models.py, save exit after editing
From django.db import modelsclass person (models. Model): name = models. Charfield (max_length=10) def __str__ (self): return Self.name
5. Synchronize change database, Makemigrations is create data file, migrate is synchronous data
Python manage.py Makemigrationspython manage.py Migrate
Terminal Display
6. View the database, switch to the original database, we can find that the class we created is already in the database, that is Blog_person
Terminal commands: Use MYSQL1;, and then using show tables;
Third, review the database configuration ideas:
1. Go to MySQL database to create a database
2. Create the project and the app, settings.py set the database type, database name and user name, and password in the project
3. models.py create data in the application, create data files and synchronize data in the terminal
Series Prev: Django Learning Notes (v) Template tags
Next Article series:
Django Learning Notes (vi) MySQL configuration