From a4a76c1846caef8007e8b98d43e1a2ec1fcdf905 Mon Sep 17 00:00:00 2001 From: zhangshu <70257536+Appolli9527@users.noreply.github.com> Date: Thu, 15 Aug 2024 23:13:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Contant.py | 2 +- init_channel.py | 26 +++++++++++++++++++++++++- init_channel_config.json | 1 + 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Contant.py b/Contant.py index 669e0d2..95bab87 100644 --- a/Contant.py +++ b/Contant.py @@ -1,3 +1,3 @@ -db="" +connection="" logDir="" logFileName="" \ No newline at end of file diff --git a/init_channel.py b/init_channel.py index cc9f7ac..577d51b 100644 --- a/init_channel.py +++ b/init_channel.py @@ -3,6 +3,8 @@ from bs4 import BeautifulSoup as bs from urllib.request import urlopen, Request import json import Contant +import mysql.connector +from mysql.connector import Error # py .\init.py --db=../db/youtube_prod.db --logDir=./logs if __name__ == "__main__": @@ -10,7 +12,29 @@ if __name__ == "__main__": with open('init_channel_config.json', 'r', encoding='utf-8') as f: # 使用json.load()方法读取文件内容 data = json.load(f) + + # 初始化日志 Contant.logDir = data['log']['dir'] Contant.logFileName = data['log']['fileName'] initLogger(Contant.logDir, Contant.logFileName) - Logger.info(data['mysql']) \ No newline at end of file + + # 连接mysql + dbHost = data['mysql']['host'] + dbPort = data['mysql']['port'] + dbUserName = data['mysql']['username'] + dbPassword = data['mysql']['password'] + dbDatabase = data['mysql']['database'] + Logger.info("尝试连接mysql host:'{}' port:'{}' username:'{}' password:'{}' database:'{}'", + dbHost, dbPort, dbUserName, dbPassword, dbDatabase) + Contant.connection = None + try: + Contant.connection = mysql.connector.connect( + host=dbHost, + port=dbPort, + user=dbUserName, + passwd=dbPassword, + database=dbDatabase + ) + Logger.info("连接数据库成功") + except Error as e: + Logger.error(f"The error '{e}' occurred") diff --git a/init_channel_config.json b/init_channel_config.json index 563db15..9d020bd 100644 --- a/init_channel_config.json +++ b/init_channel_config.json @@ -1,6 +1,7 @@ { "mysql": { "host": "47.108.20.249", + "port": "3306", "username": "root", "password": "casino888!", "database": "youtube"