Django+MySQL configuration: Windows+Centos

1. Windows environment construction

1. Install Python3.6+PyCharm2019

This step is omitted, there are many installation tutorials on the Internet (note: the community version cannot directly create a Django project in PyCharm, it is best to install the professional version)

2. Configure Django 2.0.6 environment

Install the Django environment in PyCharm. In order not to mix with other installation environments, we create a new virtual environment
①Files->settings->Project:XXXX->Project Interpreter->add
Insert picture description here

②New environment (choose the location of the Python environment we installed)
Insert picture description here

③Enter the newly created environment, as shown in the figure (click the plus sign on the upper right to enter the environment installation):
Insert picture description here
④Install pip10.0.1 version
Insert picture description here

Pip can also be installed directly from the command line, as long as you enter the just-installed environment: installation environment directory\Scripts, enter

active

Insert picture description here

In this way, we can enter the virtual environment and install various packages
Insert picture description here

The following is the installation process of pip10.0.1 and django2.0.6 on the command line

python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn pip==10.0.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn django==2.0.6  

Insert picture description here
In addition, Django and the database are related, you need to install pymysql

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn pymysql 

Check the installation result in the Pycharm installation library, as shown in the following figure:
Insert picture description here

3. MySQL database installation

MySQL installation tutorial under Windows

Two, Centos environment construction

1. Install the database

(Reference: https://www.cnblogs.com/weiok/p/5373270.html) ①Here
is the MySQL YUM source download address, this version is version 5.7

wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

Insert picture description here

②After downloading, it is a mysql57-community-release-el7-7.noarch.rpm file. You can use the following command to check which packages the file contains

rpm -qpl mysql57-community-release-el7-7.noarch.rpm

Insert picture description here

③Install the rpm package

rpm -ivh mysql57-community-release-el7-7.noarch.rpm

Insert picture description here

④After installing the above packages, check the yum library

yum list Mysql*

The following packages will be generated in the yum library
Insert picture description here

Then you can install MySQL with yum:

yum install mysql-community-server

Insert picture description here

The advantage of this is that you can use yum to manage the MySQL package, especially the MySQL installation package can be generated into the YUM library, and more MYSQL installation methods. (Complete appears at the end to indicate success)

⑤ Modify the database password (reference: https://www.jb51.net/article/116032.htm) to
start mysql

service mysqld start
systemctl start mysqld.service

Check mysql running status

service mysqld status
systemctl status mysqld.service

Insert picture description here

Obtaining a temporary password
In order to strengthen security, MySQL 5.7 randomly generates a password for the root user. In the error log, the location of the error log, if the RPM package is installed, the default is:

/var/log/mysqld.log

You can view the temporary password only after starting mysql once. Use the temporary password to log in to the database. The password is the temporary password obtained in the previous step.

grep 'temporary password' /var/log/mysqld.log

Insert picture description here

Modify the password, use the default password to log in to modify
Enter the MySQL database, execute the following statement

mysql -u root -p

Insert picture description here

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123'

Insert picture description here
The password is too simple to report an error, modify the value of the validate_password_policy parameter
First, modify the value of the validate_password_policy parameter

set global validate_password_policy=0;// 修改参数的值
set global validate_password_length=1;// 再修改密码的长度

Insert picture description here
Execute again to change the password

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123';

Insert picture description here
⑥Create a database and authorize it to other users

CREATE DATABASE SmartCom;
GRANT ALL PRIVILEGES ON *.* TO 'SmartCom'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;//使修改生效

Insert picture description here
⑦Authorize other machines to log in (this step is not necessary, only open a database to users)

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;//使修改生效

⑧Verify that the server database permissions are turned on, MySQL must be installed locally, and the environment has been configured (enter the following command locally)
Insert picture description here

2.Python installation

① Install python dependency package

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel

Insert picture description here

yum install gcc

Insert picture description here

② Install python3.6.5

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

Insert picture description here

Create soft connection

mkdir -p /usr/local/python365
tar zxvf Python-3.6.5.tgz
cd Python-3.6.5

Insert picture description here

./configure --prefix=/usr/local/python365
make
make install
ln -s /usr/local/python365/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python365/bin/pip3 /usr/local/bin/pip3
pip3 install --upgrade pip  #更新pip
pip3 install ipython  #安装ipython方便调试

Insert picture description here
Insert picture description here

3. Django installation

① Install Django

pip3 install django==2.0.6
ln -s /usr/local/python365/bin/django-admin /usr/local/bin/django-admin

Insert picture description here

②Execute the Django environment,
first perform database migration

cd /JustCloud
python manage.py makemigrations 

Insert picture description here
The following error will appear:
Insert picture description here
Solution:

vim /usr/local/python365/lib/python3.6/site-packages/django/db/backends/mysql/base.py

Enter the file and log out a certain part, as shown in the figure below:
Insert picture description here
Insert picture description here

Continue migration after comment

python3 manage.py makemigrations//这一步会记录关于model.py的改动
python3 manage.py migrate//把改动作用到指定数据库并在数据库中生成表。

Insert picture description here

③Let the main program always run instructions in the background

nohup python3 manage.py runserver 0.0.0.0:8000 >record.log 2>&1 &

④Close the main
program running in the background, first query the thread ID of the main program running

netstat -lnp|grep 00

Insert picture description here
Then close this thread, my current thread ID is 8369, so I execute the following command

kill -9 8396

Guess you like

Origin blog.csdn.net/qq_39714045/article/details/109877808