Python+Django+Ansible Playbook自動化運維

bluetooth發表於2021-09-09

環境:Ubuntu18,python 3.6  django 3.1


john@Vbox:~$ ls

download          PycharmProjects  模板  圖片  下載  桌面

examples.desktop  公共的           影片  文件  音樂


john@Vbox:~$ hostnamectl 

   Static hostname: Vbox

         Icon name: computer-vm

           Chassis: vm

        Machine ID: b9d3a184bc8c481988f16da59cf27c32

           Boot ID: 1cd67b6c9fb64fd198a0001b15716fc0

    Virtualization: oracle

  Operating System: Ubuntu 18.04.5 LTS

            Kernel: Linux 5.4.0-42-generic

      Architecture: x86-64

john@Vbox:~$ cat /etc/hosts

127.0.0.1 localhost

127.0.1.1 john-Vbox


# The following lines are desirable for IPv6 capable hosts

::1     ip6-localhost ip6-loopback

fe00::0 ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

john@Vbox:~$ sudo -i

[sudo] john 的密碼: 

root@Vbox:~# vim /etc/hosts

root@Vbox:~# ls

root@Vbox:~# pwd

/root

root@Vbox:~# ls

root@Vbox:~# ll

總用量 28

drwx------  3 root root 4096 8月  20 14:48 ./

drwxr-xr-x 24 root root 4096 8月  19 22:01 ../

-rw-------  1 root root  618 8月  20 14:47 .bash_history

-rw-r--r--  1 root root 3106 4月   9  2018 .bashrc

drwx------  2 root root 4096 8月   7 06:40 .cache/

-rw-r--r--  1 root root  148 8月  17  2015 .profile

-rw-------  1 root root  849 8月  20 14:48 .viminfo

root@Vbox:~# vim .profile 

root@Vbox:~# apt-get install python3-pip

正在讀取軟體包列表... 完成

正在分析軟體包的依賴關係樹       

正在讀取狀態資訊... 完成       

將會同時安裝下列軟體:

  build-essential dh-python dpkg-dev fakeroot g++ g++-7 gcc gcc-7

  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl

  libasan4 libatomic1 libc-dev-bin libc6-dev libcilkrts5 libexpat1-dev

  libfakeroot libgcc-7-dev libitm1 liblsan0 libmpx2 libpython3-dev

  libpython3.6-dev libquadmath0 libstdc++-7-dev libtsan0 libubsan0

  linux-libc-dev make manpages-dev python-pip-whl python3-dev

  python3-distutils python3-lib2to3 python3-setuptools python3-wheel

  python3.6-dev

建議安裝:

  debian-keyring g++-multilib g++-7-multilib gcc-7-doc libstdc++6-7-dbg

  gcc-multilib autoconf automake libtool flex bison gcc-doc gcc-7-multilib

  gcc-7-locales libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg

  libasan4-dbg liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg

  libmpx2-dbg libquadmath0-dbg glibc-doc libstdc++-7-doc make-doc

  python-setuptools-doc

下列【新】軟體包將被安裝:

  build-essential dh-python dpkg-dev fakeroot g++ g++-7 gcc gcc-7

  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl

  libasan4 libatomic1 libc-dev-bin libc6-dev libcilkrts5 libexpat1-dev

  libfakeroot libgcc-7-dev libitm1 liblsan0 libmpx2 libpython3-dev

  libpython3.6-dev libquadmath0 libstdc++-7-dev libtsan0 libubsan0

  linux-libc-dev make manpages-dev python-pip-whl python3-dev

  python3-distutils python3-lib2to3 python3-pip python3-setuptools

  python3-wheel python3.6-dev

升級了 0 個軟體包,新安裝了 39 個軟體包,要解除安裝 0 個軟體包,有 8 個軟體包未被升級。

需要下載 78.7 MB 的歸檔。

解壓縮後會消耗 206 MB 的額外空間。

您希望繼續執行嗎? [Y/n] y


安裝過程略


正在設定 python3-dev (3.6.7-1~18.04) ...

正在處理用於 man-db (2.8.3-2ubuntu0.1) 的觸發器 ...

正在處理用於 libc-bin (2.27-3ubuntu1.2) 的觸發器 ...

root@Vbox:~# pip3 install requests

Requirement already satisfied: requests in /usr/lib/python3/dist-packages

root@Vbox:~# python3

Python 3.6.9 (default, Jul 17 2020, 12:50:27) 

[GCC 8.4.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import requests

>>> exit

Use exit() or Ctrl-D (i.e. EOF) to exit

>>> 

#安裝django,預設的是3.1的最新版本,其他版本請用django==x.x

root@Vbox:~# pip3 install django

Collecting django

  Downloading (7.8MB)

    34% |███████████                     | 2.7MB 5.4kB/s eta 0:15:54Exception:



Successfully installed asgiref-3.2.10 django-3.1 sqlparse-0.3.1

root@Vbox:~# 

root@Vbox:~# 

root@Vbox:~# 

root@Vbox:~# 

#安裝mysql-server

root@Vbox:~# apt-get install mysql-server

正在讀取軟體包列表... 完成

正在分析軟體包的依賴關係樹       

正在讀取狀態資訊... 完成       

將會同時安裝下列軟體:

  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7

  mysql-client-core-5.7 mysql-server-5.7 mysql-server-core-5.7

建議安裝:

  libipc-sharedcache-perl mailx tinyca

下列【新】軟體包將被安裝:

  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7

  mysql-client-core-5.7 mysql-server mysql-server-5.7 mysql-server-core-5.7

升級了 0 個軟體包,新安裝了 8 個軟體包,要解除安裝 0 個軟體包,有 8 個軟體包未被升級。

需要下載 19.1 MB 的歸檔。

解壓縮後會消耗 155 MB 的額外空間。

您希望繼續執行嗎? [Y/n] y

過程略


Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.

正在設定 mysql-server (5.7.31-0ubuntu0.18.04.1) ...

正在處理用於 libc-bin (2.27-3ubuntu1.2) 的觸發器 ...

正在處理用於 systemd (237-3ubuntu10.42) 的觸發器 ...

正在處理用於 man-db (2.8.3-2ubuntu0.1) 的觸發器 ...

正在處理用於 ureadahead (0.100.0-21) 的觸發器 ...


#安裝MongoDB

root@Vbox:~# apt-get install mongodb

正在讀取軟體包列表... 完成

正在分析軟體包的依賴關係樹       

正在讀取狀態資訊... 完成       

將會同時安裝下列軟體:

  libboost-program-options1.65.1 libgoogle-perftools4 libpcrecpp0v5

  libsnappy1v5 libtcmalloc-minimal4 libyaml-cpp0.5v5 mongo-tools

  mongodb-clients mongodb-server mongodb-server-core

下列【新】軟體包將被安裝:

  libboost-program-options1.65.1 libgoogle-perftools4 libpcrecpp0v5

  libsnappy1v5 libtcmalloc-minimal4 libyaml-cpp0.5v5 mongo-tools mongodb

  mongodb-clients mongodb-server mongodb-server-core

升級了 0 個軟體包,新安裝了 11 個軟體包,要解除安裝 0 個軟體包,有 8 個軟體包未被升級。

需要下載 53.4 MB 的歸檔。

解壓縮後會消耗 217 MB 的額外空間。

您希望繼續執行嗎? [Y/n] y

獲取:1 bionic/main amd64 libboost-program-options1.65.1 amd64 1.65.1+dfsg-0ubuntu5 [137 kB]


正在設定 mongodb (1:3.6.3-0ubuntu1.1) ...

正在處理用於 systemd (237-3ubuntu10.42) 的觸發器 ...

正在處理用於 man-db (2.8.3-2ubuntu0.1) 的觸發器 ...

正在處理用於 ureadahead (0.100.0-21) 的觸發器 ...

正在處理用於 libc-bin (2.27-3ubuntu1.2) 的觸發器 ...


#安裝redis

root@Vbox:~# apt-get install redis

正在讀取軟體包列表... 完成

正在分析軟體包的依賴關係樹       

正在讀取狀態資訊... 完成       

將會同時安裝下列軟體:

  libjemalloc1 redis-server redis-tools

建議安裝:

  ruby-redis

下列【新】軟體包將被安裝:

  libjemalloc1 redis redis-server redis-tools

升級了 0 個軟體包,新安裝了 4 個軟體包,要解除安裝 0 個軟體包,有 8 個軟體包未被升級。

需要下載 637 kB 的歸檔。

解壓縮後會消耗 3,083 kB 的額外空間。

您希望繼續執行嗎? [Y/n] y 

獲取:1 bionic/universe amd64 libjemalloc1 amd64 3.6.0-11 [82.4 kB]

獲取:2 bionic-updates/universe amd64 redis-tools amd64 5:4.0.9-1ubuntu0.2 [516 kB]

獲取:3 bionic-updates/universe amd64 redis-server amd64 5:4.0.9-1ubuntu0.2 [35.4 kB]

獲取:4 bionic-updates/universe amd64 redis all 5:4.0.9-1ubuntu0.2 [3,084 B]

已下載 637 kB,耗時 17秒 (36.4 kB/s)                                           

正在選中未選擇的軟體包 libjemalloc1。

(正在讀取資料庫 ... 系統當前共安裝有 142154 個檔案和目錄。)

正準備解包 .../libjemalloc1_3.6.0-11_amd64.deb  ...

正在解包 libjemalloc1 (3.6.0-11) ...

正在選中未選擇的軟體包 redis-tools。

正準備解包 .../redis-tools_5%3a4.0.9-1ubuntu0.2_amd64.deb  ...

正在解包 redis-tools (5:4.0.9-1ubuntu0.2) ...

正在選中未選擇的軟體包 redis-server。

正準備解包 .../redis-server_5%3a4.0.9-1ubuntu0.2_amd64.deb  ...

正在解包 redis-server (5:4.0.9-1ubuntu0.2) ...

正在選中未選擇的軟體包 redis。

正準備解包 .../redis_5%3a4.0.9-1ubuntu0.2_all.deb  ...

正在解包 redis (5:4.0.9-1ubuntu0.2) ...

正在設定 libjemalloc1 (3.6.0-11) ...

正在設定 redis-tools (5:4.0.9-1ubuntu0.2) ...

正在設定 redis-server (5:4.0.9-1ubuntu0.2) ...

Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-server.service.

Created symlink /etc/systemd/system/multi-user.target.wants/redis-server.service → /lib/systemd/system/redis-server.service.

正在設定 redis (5:4.0.9-1ubuntu0.2) ...

正在處理用於 libc-bin (2.27-3ubuntu1.2) 的觸發器 ...

正在處理用於 systemd (237-3ubuntu10.42) 的觸發器 ...

正在處理用於 man-db (2.8.3-2ubuntu0.1) 的觸發器 ...

正在處理用於 ureadahead (0.100.0-21) 的觸發器 ...

 

#啟動測試下mongo

     

root@Vbox:~# mongo start

MongoDB shell version v3.6.3

connecting to: mongodb://127.0.0.1:27017/start

MongoDB server version: 3.6.3

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

Questions? Try the support group

Server has startup warnings: 

2020-08-20T16:20:45.618+0800 I STORAGE  [initandlisten] 

2020-08-20T16:20:45.618+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine

2020-08-20T16:20:45.618+0800 I STORAGE  [initandlisten] **          See

2020-08-20T16:20:46.292+0800 I CONTROL  [initandlisten] 

2020-08-20T16:20:46.292+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2020-08-20T16:20:46.292+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2020-08-20T16:20:46.292+0800 I CONTROL  [initandlisten] 

> q

2020-08-20T16:25:15.546+0800 E QUERY    [thread1] ReferenceError: q is not defined :

@(shell):1:1

> exit

bye

#使用systemctl控制伺服器

root@Vbox:~# systemctl start mongodb.service 

root@Vbox:~# systemctl status  mongodb.service 

● mongodb.service - An object/document-oriented database

   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: 

   Active: active (running) since Thu 2020-08-20 16:20:45 CST; 4min 47s ago

     Docs: man:mongod(1)

 Main PID: 18602 (mongod)

    Tasks: 23 (limit: 2326)

   CGroup: /system.slice/mongodb.service

           └─18602 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc


8月 20 16:20:45 Vbox systemd[1]: Started An object/document-oriented database.

#修改下繫結ip為 0.0.0.0

root@Vbox:~# vim /etc/mongodb.conf 

root@Vbox:~# systemctl restart  mongodb.service 


#檢視是否是 0.0.0.0的繫結ip

root@Vbox:~# netstat -antlp|grep mon

tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      20325/mongod        

root@Vbox:~# netstat -anp|grep mon

tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      20325/mongod        


#啟動redis 

root@Vbox:~# systemctl start redis

redis-server@         redis-server.service  redis.service

root@Vbox:~# systemctl start redis-server

redis-server@         redis-server.service  

root@Vbox:~# systemctl start redis-server.service 

root@Vbox:~# systemctl status redis-server.service 

● redis-server.service - Advanced key-value store

   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor pre

   Active: active (running) since Thu 2020-08-20 16:24:27 CST; 2min 59s ago

     Docs: http://redis.io/documentation,

           man:redis-server(1)

 Main PID: 19566 (redis-server)

    Tasks: 4 (limit: 2326)

   CGroup: /system.slice/redis-server.service

           └─19566 /usr/bin/redis-server 127.0.0.1:6379


8月 20 16:24:27 Vbox systemd[1]: Starting Advanced key-value store...

8月 20 16:24:27 Vbox systemd[1]: Started Advanced key-value store.


#修改reids的繫結ip為 0.0.0.0

root@Vbox:~# vim /etc/redis/redis.conf 

root@Vbox:~# netstat -antlp |grep redis

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      19566/redis-server  

tcp6       0      0 ::1:6379                :::*                    LISTEN      19566/redis-server  

root@Vbox:~# systemctl restart redis-server.service 

root@Vbox:~# netstat -antlp |grep redis

tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      20533/redis-server  

tcp6       0      0 ::1:6379                :::*                    LISTEN      20533/redis-server  

root@Vbox:~# redi

rediff           redis-check-aof  redis-cli        

redis-benchmark  redis-check-rdb  redis-server     

#連線 redis測試,生產

root@Vbox:~# redis-cli 

127.0.0.1:6379> help

redis-cli 4.0.9

To get help about Redis commands type:

      "help @<group>" to get a list of commands in <group>

      "help <command>" for help on <command>

      "help <tab>" to get a list of possible help topics

      "quit" to exit


127.0.0.1:6379> exit


#設定mysql的root,及清理不必要的許可權及表

root@Vbox:~# mysql_secure_installation 


Securing the MySQL server deployment.


Connecting to MySQL using a blank password.


VALIDATE PASSWORD PLUGIN can be used to test passwords

and improve security. It checks the strength of password

and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD plugin?


Press y|Y for Yes, any other key for No: y


There are three levels of password validation policy:


LOW    Length >= 8

MEDIUM Length >= 8, numeric, mixed case, and special characters

STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file


Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Please set the password for root here.


New password: 


Re-enter new password: 


Estimated strength of the password: 25 

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them. This is intended only for

testing, and to make the installation go a bit smoother.

You should remove them before moving into a production

environment.


Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Success.



Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network.


Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Success.


By default, MySQL comes with a database named 'test' that

anyone can access. This is also intended only for testing,

and should be removed before moving into a production

environment.



Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

 - Dropping test database...

Success.


 - Removing privileges on test database...

Success.


Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.


Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

Success.


All done! 


#測試使用新密碼連線

root@Vbox:~# mysql -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 4

Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)


Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.


mysql> eixt

    -> q

q

^C

mysql> exit

Bye

# 將下載後的python3的專案下載下來,放到新建的/opt/code/ 下 並解壓

root@Vbox:~# cd /opt/code/


root@Vbox:/opt/code# cp /home/john/download/imoocc_python3_online.tgz  .


root@Vbox:/opt/code# gunzip imoocc_python3_online.tgz 

root@Vbox:/opt/code# ls

imoocc_python3_online.tar


root@Vbox:/opt/code# file imoocc_python3_online.tar 

imoocc_python3_online.tar: POSIX tar archive

root@Vbox:/opt/code# tar -xvf imoocc_python3_online.tar ./imoocc_py3_online/

./imoocc_py3_online/

./imoocc_py3_online/taskdo/

./imoocc_py3_online/error.logs

./imoocc_py3_online/requirements.txt

./imoocc_py3_online/._admin

./imoocc_py3_online/admin/

略.......



./imoocc_py3_online/taskdo/migrations/__pycache__/__init__.cpython-36.pyc


#解壓完畢後,檢視內容

root@Vbox:/opt/code# ls

imoocc_py3_online  imoocc_python3_online.tar

root@Vbox:/opt/code# cd imoocc_py3_online/

root@Vbox:/opt/code/imoocc_py3_online# ls

admin  error.logs   logs       README.md         script  templates

apps   extra_apps   main.py    requirements.txt  static

conf   __init__.py  manage.py  scanhosts         taskdo

root@Vbox:/opt/code/imoocc_py3_online# cd ..

root@Vbox:/opt/code# ls

imoocc_py3_online  imoocc_python3_online.tar

root@Vbox:/opt/code# mv imoocc_py

imoocc_py3_online/         imoocc_python3_online.tar  


#為了跟影片的名稱一樣,我修改了下資料夾名稱為 imoocc

root@Vbox:/opt/code# mv imoocc_py3_online/ imoocc

root@Vbox:/opt/code# ll

總用量 9672

drwxr-xr-x  3 root root     4096 8月  20 17:25 ./

drwxr-xr-x  3 root root     4096 8月  20 17:21 ../

drwxr-xr-x 13  501 staff    4096 4月  19  2018 imoocc/

-rw-r--r--  1 root root  9891840 8月  20 17:22 imoocc_python3_online.tar

root@Vbox:/opt/code# cd imoocc/

root@Vbox:/opt/code/imoocc# 

root@Vbox:/opt/code/imoocc# vim admin/

__init__.py   __init__.pyc  __pycache__/  settings/     urls.py       urls.pyc      wsgi.py       wsgi.pyc    

#修改下  資料庫的配置資訊,包括mysql的,mongo,redis的配置也在這裡

#修改資料庫名稱,使用者名稱密碼埠等。


root@Vbox:/opt/code/imoocc# vim admin/settings/settings.py


#修改mysqld 的字符集

root@Vbox:/opt/code/imoocc# vim /etc/mysql/my.cnf


#新增如下內容

[client]

default-character-set=utf8


[mysqld]

character-set-server=utf8

collation-server=utf8_general_ci



#重啟資料庫

root@Vbox:/opt/code/imoocc# systemctl restart mysql.service 



root@Vbox:/opt/code/imoocc# mysql -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 3

Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)


Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.


mysql> ls

    -> ;

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 'ls' at line 1

mysql> 

mysql> create database imoocc;

Query OK, 1 row affected (0.00 sec)



mysql>  grant all on imoocc.* to imoocc@'127.0.0.1' identified by '123456';

Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> exit


#使用新使用者登陸測試

root@Vbox:/opt/code/imoocc# mysql -u imoocc -p -h 127.0.0.1

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 9

Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)


Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.


mysql> exit

Bye


#執行遷移:原則是發現啥錯誤,改啥錯誤,沒啥模組,裝啥模組。

root@Vbox:/opt/code/imoocc# python3 manage.py makemigrations

Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in run_from_argv

 

  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

  File "/opt/code/imoocc/admin/__init__.py", line 1, in <module>

    import pymysql

ModuleNotFoundError: No module named 'pymysql'


ModuleNotFoundError: No module named 'pymysql'

root@Vbox:/opt/code/imoocc# pip3 install pymysql

Collecting pymysql





  Could not find a version that satisfies the requirement pymysql (from versions: )

No matching distribution found for pymysql


root@Vbox:/opt/code/imoocc# 

#裝pymysql,這個版本很低,導致後面有個判斷直接讀這個版本

root@Vbox:/opt/code/imoocc# pip3 install pymysql

Collecting pymysql

  Downloading (47kB)

    100% |████████████████████████████████| 51kB 3.8kB/s 

Installing collected packages: pymysql

Successfully installed pymysql-0.10.0

root@Vbox:/opt/code/imoocc# 


root@Vbox:/opt/code/imoocc# python3 manage.py makemigrations

Traceback (most recent call last):

  File "manage.py", line 15, in <module>

    execute_from_command_line(sys.argv)

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line

    utility.execute()

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 377, in execute

    django.setup()

  File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 91, in populate

    app_config = AppConfig.create(entry)

  File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 90, in create

    module = import_module(entry)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 994, in _gcd_import

  File "<frozen importlib._bootstrap>", line 971, in _find_and_load

  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'django_crontab'

#裝 django_crontab

root@Vbox:/opt/code/imoocc# pip3 install django_crontab

Collecting django_crontab

  Downloading

Requirement already satisfied: Django>=1.8 in /usr/local/lib/python3.6/dist-packages (from django_crontab)

Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.6/dist-packages (from Django>=1.8->django_crontab)

Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from Django>=1.8->django_crontab)

Requirement already satisfied: asgiref~=3.2.10 in /usr/local/lib/python3.6/dist-packages (from Django>=1.8->django_crontab)

Building wheels for collected packages: django-crontab

  Running setup.py bdist_wheel for django-crontab ... done

  Stored in directory: /root/.cache/pip/wheels/24/f9/cc/62f87c4bc00383af995adc4a870e4d74cc1d632a172e548679

Successfully built django-crontab

Installing collected packages: django-crontab

Successfully installed django-crontab-0.7.1

root@Vbox:/opt/code/imoocc# python3 manage.py makemigrations

Traceback (most recent call last):

  File "manage.py", line 15, in <module>

    execute_from_command_line(sys.argv)

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line

    utility.execute()

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 377, in execute

    django.setup()

  File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 91, in populate

    app_config = AppConfig.create(entry)

  File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 90, in create

    module = import_module(entry)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 994, in _gcd_import

  File "<frozen importlib._bootstrap>", line 971, in _find_and_load

  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'crispy_forms'


裝 crispy_forms,但是名稱不是這個,提示找不到,應該是django-crispy-forms

root@Vbox:/opt/code/imoocc# pip3 install crispy_forms

Collecting crispy_forms

Exception:

Traceback (most recent call last):

  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main


root@Vbox:/opt/code/imoocc# pip3 install django-crispy-forms

Collecting django-crispy-forms

  Downloading (108kB)

    100% |████████████████████████████████| 112kB 4.0kB/s 

Installing collected packages: django-crispy-forms

Successfully installed django-crispy-forms-1.9.2

root@Vbox:/opt/code/imoocc# python3 manage.py makemigrations

Traceback (most recent call last):

  File "manage.py", line 15, in <module>

    execute_from_command_line(sys.argv)

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line

    utility.execute()

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 377, in execute

    django.setup()

  File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 114, in populate

    app_config.import_models()

  File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 211, in import_models

    self.models_module = import_module(models_module_name)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 994, in _gcd_import

  File "<frozen importlib._bootstrap>", line 971, in _find_and_load

  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked

  File "<frozen importlib._bootstrap_external>", line 678, in exec_module

  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

  File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/models.py", line 2, in <module>

    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager

  File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/base_user.py", line 48, in <module>

    class AbstractBaseUser(models.Model):

  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 122, in __new__

    new_class.add_to_class('_meta', Options(meta, app_label))

  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 326, in add_to_class

    value.contribute_to_class(cls, name)

  File "/usr/local/lib/python3.6/dist-packages/django/db/models/options.py", line 206, in contribute_to_class

    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

  File "/usr/local/lib/python3.6/dist-packages/django/db/__init__.py", line 28, in __getattr__

    return getattr(connections[DEFAULT_DB_ALIAS], item)

  File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 214, in __getitem__

    backend = load_backend(db['ENGINE'])

  File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 111, in load_backend

    return import_module('%s.base' % backend_name)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py", line 36, in <module>

    raise ImproperlyConfigured('mysqlclient 1.4.0 or newer is required; you have %s.' % Database.__version__)

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.0.


#就是這個mysqlclient版本,總是讀的pymysql中的mysqlclient的版本,是0.10.0,後續裝了幾個模組,都沒搞定,只能遮蔽對應的程式碼繞過了

root@Vbox:/opt/code/imoocc# 

root@Vbox:/opt/code/imoocc# pip3 install pymysql

Requirement already satisfied: pymysql in /usr/local/lib/python3.6/dist-packages

root@Vbox:/opt/code/imoocc# python3

Python 3.6.9 (default, Jul 17 2020, 12:50:27) 

[GCC 8.4.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import pymysql

>>> dir()

['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'pymysql']

>>> dir(pymysql)

['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'DATETIME', 'DBAPISet', 'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'OperationalError', 'PY2', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'VERSION', 'VERSION_STRING', 'Warning', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_auth', '_compat', 'apilevel', 'charset', 'connect', 'connections', 'constants', 'converters', 'cursors', 'err', 'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info', 'install_as_MySQLdb', 'optionfile', 'paramstyle', 'protocol', 'sys', 'thread_safe', 'threadsafety', 'times', 'util', 'version_info']

>>> pymysql.version_info

(1, 3, 13, 'final', 0)

>>> exit

Use exit() or Ctrl-D (i.e. EOF) to exit

>>> 



#安裝了MySQL-python嘗試

root@Vbox:/opt/code/imoocc# pip3 install MySQL-python

Collecting MySQL-python

  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. (read timeout=15)",)': /simple/mysql-python/

  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)': /packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip

  Downloading (108kB)

    100% |████████████████████████████████| 112kB 4.2kB/s 

    Complete output from command python setup.py egg_info:

    Traceback (most recent call last):

      File "<string>", line 1, in <module>

      File "/tmp/pip-build-k_73p2c0/MySQL-python/setup.py", line 13, in <module>

        from setup_posix import get_config

      File "/tmp/pip-build-k_73p2c0/MySQL-python/setup_posix.py", line 2, in <module>

        from ConfigParser import SafeConfigParser

    ModuleNotFoundError: No module named 'ConfigParser'

    

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

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-k_73p2c0/MySQL-python/

#安裝了mysqlclient嘗試

root@Vbox:/opt/code/imoocc# pip3 install mysqlclient

Collecting mysqlclient

  Downloading (87kB)

    100% |████████████████████████████████| 92kB 2.2kB/s 

    Complete output from command python setup.py egg_info:

    /bin/sh: 1: mysql_config: not found

    /bin/sh: 1: mariadb_config: not found

    /bin/sh: 1: mysql_config: not found

    Traceback (most recent call last):

      File "<string>", line 1, in <module>

      File "/tmp/pip-build-g13d2uh6/mysqlclient/setup.py", line 15, in <module>

        metadata, options = get_config()

      File "/tmp/pip-build-g13d2uh6/mysqlclient/setup_posix.py", line 65, in get_config

        libs = mysql_config("libs")

      File "/tmp/pip-build-g13d2uh6/mysqlclient/setup_posix.py", line 31, in mysql_config

        raise OSError("{} not found".format(_mysql_config_path))

    OSError: mysql_config not found

    

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

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-g13d2uh6/mysqlclient/


#安裝軟體包嘗試

root@Vbox:/opt/code/imoocc# apt-get install mysql-devel

正在讀取軟體包列表... 完成

正在分析軟體包的依賴關係樹       

正在讀取狀態資訊... 完成       

E: 無法定位軟體包 mysql-devel

root@Vbox:/opt/code/imoocc# apt-get install libmysqlclient-dev

正在讀取軟體包列表... 完成

正在分析軟體包的依賴關係樹       

正在讀取狀態資訊... 完成       

將會同時安裝下列軟體:

  libssl-dev zlib1g-dev

建議安裝:

  libssl-doc

下列【新】軟體包將被安裝:

  libmysqlclient-dev libssl-dev zlib1g-dev

升級了 0 個軟體包,新安裝了 3 個軟體包,要解除安裝 0 個軟體包,有 8 個軟體包未被升級。

需要下載 2,733 kB 的歸檔。

解壓縮後會消耗 14.3 MB 的額外空間。

您希望繼續執行嗎? [Y/n] y

獲取:1 bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.6 [1,566 kB]

獲取:1 bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.6 [1,566 kB]                  

獲取:1 bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.6 [1,566 kB]                  

獲取:1 bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.6 [1,566 kB]                  

獲取:1 bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.6 [1,566 kB]                  

獲取:1 bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.6 [1,566 kB]                  

獲取:2 bionic/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-0ubuntu2 [176 kB]                              

獲取:3 bionic-updates/main amd64 libmysqlclient-dev amd64 5.7.31-0ubuntu0.18.04.1 [992 kB]             

獲取:3 bionic-updates/main amd64 libmysqlclient-dev amd64 5.7.31-0ubuntu0.18.04.1 [992 kB]             

已下載 1,532 kB,耗時 6分 1秒 (4,245 B/s)    

正在選中未選擇的軟體包 libssl-dev:amd64。

(正在讀取資料庫 ... 系統當前共安裝有 142200 個檔案和目錄。)

正準備解包 .../libssl-dev_1.1.1-1ubuntu2.1~18.04.6_amd64.deb  ...

正在解包 libssl-dev:amd64 (1.1.1-1ubuntu2.1~18.04.6) ...

正在選中未選擇的軟體包 zlib1g-dev:amd64。

正準備解包 .../zlib1g-dev_1%3a1.2.11.dfsg-0ubuntu2_amd64.deb  ...

正在解包 zlib1g-dev:amd64 (1:1.2.11.dfsg-0ubuntu2) ...

正在選中未選擇的軟體包 libmysqlclient-dev。

正準備解包 .../libmysqlclient-dev_5.7.31-0ubuntu0.18.04.1_amd64.deb  ...

正在解包 libmysqlclient-dev (5.7.31-0ubuntu0.18.04.1) ...

正在設定 libssl-dev:amd64 (1.1.1-1ubuntu2.1~18.04.6) ...

正在設定 zlib1g-dev:amd64 (1:1.2.11.dfsg-0ubuntu2) ...

正在設定 libmysqlclient-dev (5.7.31-0ubuntu0.18.04.1) ...

正在處理用於 man-db (2.8.3-2ubuntu0.1) 的觸發器 ...


root@Vbox:/opt/code/imoocc# python3 manage.py makemigrations

Traceback (most recent call last):

  File "manage.py", line 15, in <module>

    execute_from_command_line(sys.argv)

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line

    utility.execute()

  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 377, in execute

    django.setup()

  File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 114, in populate

    app_config.import_models()

  File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 211, in import_models

    self.models_module = import_module(models_module_name)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 994, in _gcd_import

  File "<frozen importlib._bootstrap>", line 971, in _find_and_load

  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked

  File "<frozen importlib._bootstrap_external>", line 678, in exec_module

  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

  File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/models.py", line 2, in <module>

    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager

  File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/base_user.py", line 48, in <module>

    class AbstractBaseUser(models.Model):

  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 122, in __new__

    new_class.add_to_class('_meta', Options(meta, app_label))

  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 326, in add_to_class

    value.contribute_to_class(cls, name)

  File "/usr/local/lib/python3.6/dist-packages/django/db/models/options.py", line 206, in contribute_to_class

    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

  File "/usr/local/lib/python3.6/dist-packages/django/db/__init__.py", line 28, in __getattr__

    return getattr(connections[DEFAULT_DB_ALIAS], item)

  File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 214, in __getitem__

    backend = load_backend(db['ENGINE'])

  File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 111, in load_backend

    return import_module('%s.base' % backend_name)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py", line 36, in <module>

    raise ImproperlyConfigured('mysqlclient 1.4.0 or newer is required; you have %s.' % Database.__version__)

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.0.

root@Vbox:/opt/code/imoocc# vim /usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3402/viewspace-2826099/,如需轉載,請註明出處,否則將追究法律責任。

相關文章