New Django project and configure the mysql database on the cloud server (CentOS 7.6 system)

Recently Tencent cloud spend 5 yuan a month to rent the cloud server's domain name + year, thinking Django project will be deployed to the cloud server, fiddle for a long time before they realized the Configuration section of the cloud environment.

table of Contents

python3 installation

yum configuration

Install the MySQL database

New django project

Mysql Django project configuration

They check local start on the cloud server Django project


python3 installation

CentOS 7.6 comes with a python 2.7.5 version,

[root@VM_0_3_centos ~]# python -V
Python 2.7.5

First install python3,

[root@VM_0_3_centos ~]# yum install python3

View python3 version

[root@VM_0_3_centos ~]# python3 -V
Python 3.6.8

Set the connection (similar to shortcut windows system) to connect the python to python3, first check the existing soft connection on the python

[root@VM_0_3_centos ~]# ll /bin/python*
lrwxrwxrwx 1 root root     7 Nov  5 23:09 /bin/python -> python2
lrwxrwxrwx 1 root root     9 Nov  5 23:09 /bin/python2 -> python2.7
-rwxr-xr-x 1 root root  7216 Aug  7  2019 /bin/python2.7
lrwxrwxrwx 1 root root     9 Apr  7 10:07 /bin/python3 -> python3.6
-rwxr-xr-x 2 root root 11408 Aug  8  2019 /bin/python3.6
-rwxr-xr-x 2 root root 11408 Aug  8  2019 /bin/python3.6m

Delete the original connection soft python and set the connection to python3.6

[root@VM_0_3_centos ~]# rm /bin/python
rm: remove symbolic link ‘/bin/python’? y
[root@VM_0_3_centos ~]# ln -s /bin/python3.6 /bin/python

Check again can be found in soft python has been connected to python3.6

[root@VM_0_3_centos ~]# ll /bin/python
lrwxrwxrwx 1 root root 14 Apr  7 10:17 /bin/python -> /bin/python3.6

The same method can be provided to pip3 pip flexible connector

[root@VM_0_3_centos ~]# ln -s /bin/pip3 /bin/pip
[root@VM_0_3_centos ~]# ll /bin/pip
lrwxrwxrwx 1 root root 9 Apr  7 10:33 /bin/pip -> /bin/pip3

yum configuration

After installing python3 and set up flexible connections, he encountered the first pit was found yum can not be used

[root@VM_0_3_centos ~]# yum
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

The original version used by default yum python2.7, and after changing the soft python connected, cause python3.6 yum version is used, and therefore can be changed back through vi editor, the specific method in the first row Plains after some python to add "2", and then save the exit

#!/usr/bin/python2
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
...
...
...

Meanwhile File "/ usr / libexec / urlgrabber-ext-down" also need to make the same modifications

#! /usr/bin/python2
#  A very simple external downloader
#  Copyright 2011-2012 Zdenek Pavlas
...
...
...

Install the MySQL database

yum to download and install software packages from the repository, view existing software library

[root@VM_0_3_centos ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 614 Apr  7 13:27 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Apr  7 13:27 CentOS-Epel.repo

First, we need to add MySQL Yum Repository (you can view the official website tutorial )

[root@VM_0_3_centos ~]# rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

View software library again and found that more than two

[root@VM_0_3_centos ~]# ll /etc/yum.repos.d/
total 16
-rw-r--r-- 1 root root  614 Apr  7 13:45 CentOS-Base.repo
-rw-r--r-- 1 root root  230 Apr  7 13:45 CentOS-Epel.repo
-rw-r--r-- 1 root root 2076 Apr 25  2019 mysql-community.repo
-rw-r--r-- 1 root root 2108 Apr 25  2019 mysql-community-source.repo

At the same time through rpm -qa ... View All Packages Related to mysql has been installed, the results show there is only one

[root@VM_0_3_centos HelloWorld]# rpm -qa mysql*
mysql80-community-release-el7-3.noarch

Can now be installed via yum install mysql-community-server, the command not only installed mysql server must also install additional packages

[root@VM_0_3_centos ~]# yum install mysql-community-server

After installation, use rpm -qa mysql * View, found five packages each new installation

[root@VM_0_3_centos ~]# rpm -qa mysql*
mysql80-community-release-el7-3.noarch
mysql-community-libs-8.0.19-1.el7.x86_64
mysql-community-libs-compat-8.0.19-1.el7.x86_64
mysql-community-common-8.0.19-1.el7.x86_64
mysql-community-client-8.0.19-1.el7.x86_64
mysql-community-server-8.0.19-1.el7.x86_64

Open mysql

[root@VM_0_3_centos HelloWorld]# systemctl start mysqld.service

After mysqld.log where a is generated initial password root user

[root@VM_0_3_centos ~]# cat /var/log/mysqld.log | grep password
2020-04-07T07:00:22.584068Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: IbHxyweP/2cx

Landing mysql, enter the corresponding password

[root@VM_0_3_centos ~]# mysql -uroot -p

During other operations, suggesting the need to change the password

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

Follow the prompts to make a change, 'Tianwan3!' Express the modified password

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Tianwan3!';   
Query OK, 0 rows affected (0.01 sec)

New Database HelloWorld, Django project reserved for backup.

mysql> CTEATE DATABASE HelloWorld;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CTEATE DATABASE HelloWorld' at line 1
mysql> CREATE DATABASE HelloWorld;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| HelloWorld         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

New django project

First pip install django

[root@VM_0_3_centos ~]# pip install django

Then cd to the / home directory, the new HelloWorld project

[root@VM_0_3_centos ~]# cd /home/
[root@VM_0_3_centos home]# django-admin startproject HelloWorld

Enter the tree view project structure (no tree, you can first install yum install tree)

[root@VM_0_3_centos home]# tree
.
`-- HelloWorld
    |-- HelloWorld
    |   |-- asgi.py
    |   |-- __init__.py
    |   |-- settings.py
    |   |-- urls.py
    |   `-- wsgi.py
    `-- manage.py

2 directories, 6 files

cd to the project HelloWorld, enter python manange.py runserver start

[root@VM_0_3_centos HelloWorld]# python manage.py runserver
...
...
...
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).

The results show that the error due to lower sqlite version (sqlite is a lightweight database django project used by default), CentOS 7.6 is built-in sqlite 3.7.17

[root@VM_0_3_centos HelloWorld]# rpm -qa sqlite*
sqlite-3.7.17-8.el7.x86_64

One solution may be commented out in the relevant codes File "/usr/local/lib64/python3.6/site-packages/django/db/backends/sqlite3/base.py" will

...
...
...
#def check_sqlite_version():
#    if Database.sqlite_version_info < (3, 8, 3):
#        raise ImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).' % Database.sqlite_version)


#check_sqlite_version()
...
...
...

Enter python manage.py runserver again, the successful launch!

[root@VM_0_3_centos HelloWorld]# python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 07, 2020 - 06:48:35
Django version 3.0.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Mysql Django project configuration

First you need to install a third-party library pymysql

[root@VM_0_3_centos HelloWorld]# pip install pymysql

Import pymysql HelloWorld in a subdirectory of the project in __init__.py

import pymysql
pymysql.install_as_MySQLdb()

Modify the database in a subdirectory HelloWorld in settings.py, the initial settings are as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

We modified as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'HelloWorld',
        'USER': 'root',
        'PASSWORD': 'Tianwan3!',
        'PORT': '3306',
        'HOST': '',
    }
}

Start the project, error, mistakes can be found in sqlite problem similar to the previous version of the problem

[root@VM_0_3_centos HelloWorld]# python manage.py  runserver
...
...
...
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

Open the file /usr/local/lib64/python3.6/site-packages/django/db/backends/mysql/base.py, comment out the following two lines

#if version < (1, 3, 13):
#    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

Started again, success!

[root@VM_0_3_centos HelloWorld]# python manage.py  runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, ses
sions.
Run 'python manage.py migrate' to apply them.

April 07, 2020 - 07:33:01
Django version 3.0.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

They check local start on the cloud server Django project

Start the project on the cloud server

[root@VM_0_3_centos HelloWorld]# python manage.py  runserver 0.0.0.0:8000

Local browser input 62.234.83.169:8000 (public ip + cloud server port number), the results being given, suggesting the need to .234.83.169 '62 'to ALLOWED_HOSTS

L cloud server using the vi open the project HelloWorld subdirectories in the settings.py, add the ip

ALLOWED_HOSTS = ['62.234.83.169']

Cloud server project is started again, the local browser refresh, success!

 

Published 29 original articles · won praise 26 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42216109/article/details/105358154