[toc]html
碼好python的每一篇文章.python
你們好!又有一段時間沒更新文章了,沒錯,是真的忙了。想要寫一篇文章,確實要花幾天時間的功夫,由於要保障文章的質量。mysql
後面的文章我會經過寫些基礎理論和實戰文章,畢竟幹技術的,光看理論確實會沒那麼多耐心,總之,我會穿插一些,保證各位看官滿意爲止。sql
序號 | IP地址 | 操做系統 | 安裝軟件 | 備註 |
---|---|---|---|---|
1 | 192.168.8.130 | CentOS 7.6.1810 | python3.6+django2.2 | 都運行在虛擬環境 |
2 | 192.168.8.131 | CentOS 7.6.1810 | mysql | mysql數據 |
3 | 192.168.8.1 | windows 10 | PyCharm Pro | 專業版可以使用與centos遠程同步配置 |
python環境安裝shell
pyenv虛擬環境管理python多版本和軟件庫 請參考此文。數據庫
如何更新pip
源步驟:django
[root@localhost ~]# mkdir .pip [root@localhost ~]# cd .pip/ [root@localhost .pip]# touch pip.conf [root@localhost .pip]# vim pip.conf [global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com # 添加或修改後,記得保存。
如何更新YUM
源步驟:vim
# 安裝wget,有則忽略,跳過 [root@localhost ~]# yum install wget -y # 備份yum源 [root@localhost python]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# mkdir repo_bak [root@localhost yum.repos.d]# mv *.repo repo_bak/ # 下載新的repo [root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo # 清除系統yum緩存並生成新的yum緩存 [root@localhost yum.repos.d]# yum clean all [root@localhost yum.repos.d]# yum makecache # 安裝EPEL(Extra Packages for Enterprise Linux )源 [root@localhost yum.repos.d]# yum install -y epel-release # 再次清除系統yum緩存並生成新的yum緩存 [root@localhost yum.repos.d]# yum clean all [root@localhost yum.repos.d]# yum makecache # 查看系統可用的yum源和全部的yum源 [root@localhost yum.repos.d]# yum repolist enabled
PIP源更新參考連接windows
YUM源更新參考連接centos
說明: 此臺CentOS IP地址是 192.168.8.130(經過NAT映射,能夠上互聯網)
# 安裝django2.2版本 (py369) [python@localhost Python]$ pip install django==2.2 # 查看已安裝的版本信息 (py369) [python@localhost Python]$ python -m django --version 2.2 # 安裝mysql拓展 (py369) [python@localhost Python]$ pip install mysqlclient
說明: 此臺CentOS IP地址是 192.168.8.131(經過NAT映射,能夠上互聯網)
提早下載以下安裝包,經過winscp上傳到根目錄下:
http://repo.mysql.com/yum/mys...
複製上面連接經過瀏覽器下載,速度更快。
# 安裝mysql及拓展 [root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm [root@localhost ~]# yum -y install mysql-community-server-5.7.31-1.el7.x86_64.rpm # 啓動mysql服務 [root@localhost ~]# systemctl start mysqld.service # 查看mysql服務狀態 [root@localhost ~]# systemctl status mysqld.service ...此處省略字符 # active 就表示正常在運行了 Active: active (running) since Tue 2020-07-21 10:14:23 CST; 2s ago ...此處省略字符
# 查找密碼,uQjeSi?N(2zH,這個就是缺省密碼 [root@localhost ~]# grep "password" /var/log/mysqld.log 2020-07-21T20:48:28.965616Z 1 [Note] A temporary password is generated for root@localhost: uQjeSi?N(2zH # 用缺省密碼登錄數據庫 [root@localhost ~]# mysql -uroot -p Enter password:uQjeSi?N(2zH # 輸入密碼 # 修改密碼 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'DevOps@2020'; Query OK, 0 rows affected (0.01 sec) # 受權訪問數據庫 mysql> grant all on *.* to 'root'@'%' identified by 'DevOps@2020'; Query OK, 0 rows affected, 1 warning (0.00 sec) # 刷新權限 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 建立新的數據庫 mysql> CREATE DATABASE IF NOT EXISTS devops default charset utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec) # 查看已建立的數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | devops | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) # 退出數據庫,修改配置文件,結果以下所示: [root@localhost ~]# cat /etc/my.cnf |grep -v ^#|grep -v ^$ [client] default-character-set = utf8 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid character-set-server = utf8 collation-server = utf8_general_ci # 重啓數據庫 [root@localhost ~]# service mysqld restart
windows 10下安裝mysql拓展並驗證鏈接數據庫:
C:\>pip install mysqlclient
備註: 我是經過pycharm導入模塊pymysql進行測試。
各位先簡單意淫下這種圖,應該有個初步的認識。
說明:不是咱們攻城獅理解的路由協議哈。
說明:view就是咱們要寫的python代碼
# 先安裝個tree軟件包 [python@localhost ~]$ sudo yum install tree # 建立一個新的工程project (py369) [python@localhost Python]$ django-admin startproject devops (py369) [python@localhost Python]$ tree devops devops ├── devops │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── settings.cpython-36.pyc │ │ ├── urls.cpython-36.pyc │ │ └── wsgi.cpython-36.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py
說明: pycharm將django項目下載到window電腦上進行編輯,而後再實時同步到CentOS上,pycharm編寫代碼方便。
1) 打開pycharm,點擊Tools->Deployment->Configuration
2) 建立新的服務名,參考以下配置:
3)下載遠端django的project:
4)修改自動同步配置,Tools->Deployment->Options
1)devops->devops->settings.py:
# 容許全部主機訪問django,星號表示全部主機 ALLOWED_HOSTS = ['*'] # 增長數據庫信息 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'devops', 'HOST':'192.168.8.131', 'USER':'root', 'PASSWORD':'root@123', 'PORT':3306, } } #修改語言編碼和時區 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'
2) 改完配置後,會自動同步到遠程django那裏。
摘取log部分:
2020/3/27 23:07] Automatic upload completed in 33 ms: 1 file transferred (103.6 kbit/s)
1)經過命令啓動:python manage.py runserver 0.0.0.0:8888
(py369) [python@localhost devops]$ python manage.py runserver 0.0.0.0:8888 Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). March 27, 2020 - 23:45:14 Django version 2.2, using settings 'devops.settings' Starting development server at http://0.0.0.0:8888/ Quit the server with CONTROL-C.
2)打開瀏覽器,輸入http://192.168.8.130:8888
:
3)還能夠進入後臺權限管理系統,輸入http://192.168.8.130:8888/admin
此時,尚未建立超級管理員用戶和密碼,不可以登錄。
4)接下來啓動自帶的APP admin
(py369) [python@localhost devops]$ python manage.py migrate
備註:此命令會把系統自帶的admin app數據庫寫入到mysql中。
5)登錄數據庫mariadb,看是否正常寫入
[root@localhost ~]# mysql -u root -pDevOps@2020 # 查看數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ddjsdb | | devops | | devops01 | | mysql | | performance_schema | | sys | +--------------------+ 7 rows in set (0.73 sec) # 切換到數據庫devops中 mysql> use devops # 查看數據庫devops中的表,已經成功寫入了 mysql> show tables; +----------------------------+ | Tables_in_devops | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 10 rows in set (0.00 sec)
6)建立一個管理員用戶
(py369) [python@localhost devops]$ python manage.py createcachetable # 依次輸入用戶名、郵箱地址、密碼
7)成功登錄後臺管理系統
# 再次啓動django (py369) [python@localhost devops]$ python manage.py runserver 0.0.0.0:8888
(py369) [python@localhost devops]$ python manage.py startapp hello # 手工建立urls.py 文件 (py369) [python@localhost devops]$ touch hello/urls.py # 查看app hello 樹結構 (py369) [python@localhost devops]$ tree hello hello ├── admin.py ├── apps.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py ├── urls.py └── views.py
說明:方法同章節3.1.2同樣。
1) 修改devops->devops->setting.py配置
INSTALLED_APPS = [ ...此處已省略 'hello.apps.HelloConfig', # 方式一:新增hello app信息 'hello', # 方式二:新增hello app信息 ]
2) 修改devops->hello->views.py配置
from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse('<p>hello django</p>')
3) 修改devops->hello->urls.py配置
from django.urls import path from . import views urlpatterns = [ path('hello/', views.index, name='index'), ]
4)最後修改devops->devops->urls.py配置
說明: 此urls.py爲根路由入口(即總入口)。
第一種靜態路由urls演示方法:
from hello import views urlpatterns = [ path('admin/', admin.site.urls), # 缺省 path('hello/', views.index), # 新增 ]
登錄瀏覽器,輸入http://192.168.8.130:8888/hello/,
效果以下:
第二種靜態路由urls演示方法:
from django.urls import path,include # 新增,導入include模塊 urlpatterns = [ path('admin/', admin.site.urls), # 缺省 path('hello/', include('hello.urls')), # 新增 ]
登錄瀏覽器,輸入http://192.168.8.130:8888/hello/hello,
效果以下:
若是喜歡的個人文章,歡迎關注個人公衆號:點滴技術,掃碼關注,不按期分享