224 lines
5.7 KiB
Python
224 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
验证MySQL配置脚本
|
||
|
||
检查:
|
||
1. .env文件是否存在
|
||
2. DATABASE_URL配置是否正确
|
||
3. MySQL连接是否正常
|
||
4. 数据库表结构是否正确
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
def check_env_file():
|
||
"""检查.env文件"""
|
||
print("\n" + "=" * 60)
|
||
print("检查.env文件")
|
||
print("=" * 60)
|
||
|
||
env_file = '.env'
|
||
if not os.path.exists(env_file):
|
||
print(f"❌ {env_file} 文件不存在")
|
||
print(f" 请确保在项目根目录创建 {env_file} 文件")
|
||
return None
|
||
|
||
print(f"✅ {env_file} 文件存在")
|
||
|
||
# 读取DATABASE_URL
|
||
db_url = None
|
||
with open(env_file, 'r', encoding='utf-8') as f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if line.startswith('DATABASE_URL='):
|
||
db_url = line.split('=', 1)[1].strip()
|
||
print(f"✅ DATABASE_URL已配置")
|
||
print(f" {db_url}")
|
||
break
|
||
|
||
if not db_url:
|
||
print("❌ 未找到DATABASE_URL配置")
|
||
print(" 请在.env文件中添加: DATABASE_URL=mysql+pymysql://...")
|
||
|
||
return db_url
|
||
|
||
def check_dependencies():
|
||
"""检查依赖"""
|
||
print("\n" + "=" * 60)
|
||
print("检查依赖")
|
||
print("=" * 60)
|
||
|
||
missing = []
|
||
|
||
try:
|
||
import pymysql
|
||
print(f"✅ PyMySQL (版本: {pymysql.__version__})")
|
||
except ImportError:
|
||
print("❌ PyMySQL 未安装")
|
||
missing.append("PyMySQL")
|
||
|
||
try:
|
||
import dotenv
|
||
print(f"✅ python-dotenv (版本: {dotenv.__version__})")
|
||
except ImportError:
|
||
print("❌ python-dotenv 未安装")
|
||
missing.append("python-dotenv")
|
||
|
||
if missing:
|
||
print(f"\n⚠️ 请安装缺失的依赖:")
|
||
print(f" pip install {' '.join(missing)}")
|
||
return False
|
||
|
||
return True
|
||
|
||
def test_database():
|
||
"""测试数据库"""
|
||
print("\n" + "=" * 60)
|
||
print("测试数据库连接")
|
||
print("=" * 60)
|
||
|
||
try:
|
||
from app import create_app, db
|
||
|
||
app = create_app()
|
||
|
||
with app.app_context():
|
||
# 尝试连接
|
||
print("🔄 正在连接数据库...")
|
||
db.create_all()
|
||
print("✅ 数据库连接成功!")
|
||
|
||
# 显示数据库信息
|
||
print(f"\n数据库信息:")
|
||
print(f" URI: {app.config['SQLALCHEMY_DATABASE_URI']}")
|
||
|
||
# 检查表
|
||
print(f"\n检查数据库表:")
|
||
from sqlalchemy import inspect
|
||
inspector = inspect(db.engine)
|
||
tables = inspector.get_table_names()
|
||
print(f" 数据库表数量: {len(tables)}")
|
||
for table in tables:
|
||
print(f" - {table}")
|
||
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"❌ 数据库连接失败")
|
||
print(f" 错误: {e}")
|
||
print(f"\n请检查:")
|
||
print(f" 1. MySQL服务是否运行")
|
||
print(f" 2. .env文件中的DATABASE_URL是否正确")
|
||
print(f" 3. 用户名和密码是否正确")
|
||
print(f" 4. 数据库是否存在")
|
||
return False
|
||
|
||
def check_admin_model():
|
||
"""检查Admin模型"""
|
||
print("\n" + "=" * 60)
|
||
print("检查Admin模型")
|
||
print("=" * 60)
|
||
|
||
try:
|
||
from app import create_app
|
||
from app.models import Admin
|
||
|
||
app = create_app()
|
||
|
||
with app.app_context():
|
||
# 检查字段
|
||
columns = [c.name for c in Admin.__table__.columns]
|
||
print(f"Admin表字段 ({len(columns)}个):")
|
||
for col in sorted(columns):
|
||
print(f" - {col}")
|
||
|
||
# 检查新字段
|
||
missing = []
|
||
if 'is_deleted' not in columns:
|
||
missing.append('is_deleted')
|
||
if 'delete_time' not in columns:
|
||
missing.append('delete_time')
|
||
|
||
if missing:
|
||
print(f"\n⚠️ 缺少字段: {', '.join(missing)}")
|
||
print(f" 请运行: python setup_mysql.py")
|
||
return False
|
||
|
||
print("\n✅ Admin模型结构正确")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"❌ 检查Admin模型失败: {e}")
|
||
return False
|
||
|
||
def show_config_example():
|
||
"""显示配置示例"""
|
||
print("\n" + "=" * 60)
|
||
print("配置示例")
|
||
print("=" * 60)
|
||
|
||
print("\n.env文件示例:")
|
||
print("-" * 60)
|
||
print("""# 环境配置
|
||
FLASK_ENV=development
|
||
FLASK_DEBUG=True
|
||
|
||
# 数据库配置 - MySQL
|
||
DATABASE_URL=mysql+pymysql://root:你的密码@localhost/kamaxitong
|
||
|
||
# 安全配置
|
||
SECRET_KEY=taiyi1224
|
||
AUTH_SECRET_KEY=taiyi1224
|
||
""")
|
||
print("-" * 60)
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("\n" + "=" * 60)
|
||
print("🔍 MySQL配置验证工具")
|
||
print("=" * 60)
|
||
|
||
success = True
|
||
|
||
# 1. 检查.env文件
|
||
db_url = check_env_file()
|
||
if not db_url:
|
||
show_config_example()
|
||
return 1
|
||
|
||
# 2. 检查依赖
|
||
if not check_dependencies():
|
||
success = False
|
||
|
||
# 3. 测试数据库
|
||
if success and not test_database():
|
||
success = False
|
||
|
||
# 4. 检查Admin模型
|
||
if success and not check_admin_model():
|
||
success = False
|
||
|
||
# 总结
|
||
print("\n" + "=" * 60)
|
||
if success:
|
||
print("✅ MySQL配置验证通过!")
|
||
print("=" * 60)
|
||
print("\n📖 下一步:")
|
||
print(" python run.py")
|
||
print("\n🔑 默认管理员:")
|
||
print(" 用户名: admin")
|
||
print(" 密码: admin123456")
|
||
return 0
|
||
else:
|
||
print("❌ MySQL配置验证失败")
|
||
print("=" * 60)
|
||
print("\n💡 尝试自动修复:")
|
||
print(" python setup_mysql.py")
|
||
return 1
|
||
|
||
if __name__ == '__main__':
|
||
sys.exit(main())
|