# 终端安装pymysql库 pip install pymysql
/**** 杨祥民 ****/
/**** QQ:2042870085 ****/
import pymysql
def login(un, pw):
"""用户登录"""
try:
if cr.execute(f"select * from reg_login_127 where username='{un}'and password='{pw}';") == 1:
print(f'用户{un}登录系统成功', '-' * 10)
else:
print(f'用户{un}登录系统失败', '-' * 10)
if cr.execute(f"select * from reg_login_127 where username='{un}';") == 1:
print("密码错误")
else:
print('账号不存在')
except Exception as e:
print(e)
def logon(un, pw):
"""用户注册"""
try:
"""插入数据到数据表reg_login_127中"""
cr.execute(f'insert into reg_login_127 (username,password) '
f"values ('{un}','{pw}');")
connet.commit()
print(f'用户{un}注册成功', '-' * 10)
except Exception as e:
print(e)
if __name__ == '__main__':
try:
connet = pymysql.connect(host='192.168.2.100',
user='admin',
passwd='adminpw',
database='Digital_school')
cr = connet.cursor()
except pymysql.err.OperationalError as e:
print('登录超时')
print(e)
"""检测数据表是否存在"""
try:
if cr.execute(f"select * from information_schema.TABLES WHERE TABLE_NAME = 'reg_login_127'") == 1:
print(f"数据表reg_login_127存在")
else:
cr.execute(f"create table reg_login_127(username char(20) unique key,password char(20))")
connet.commit()
print(f"数据表reg_login_127创建成功")
except Exception as e:
print(e)
options = input('1--登录\n2--注册\n')
if options == '1':
username = input('用户名')
password = input('密码')
login(username,password)
if options == '2':
username = input('用户名')
password = input('密码')
logon(username,password)
/****数据库配置****/
show databases; # 检查数据库是否存在
create database Digital_school; # 创建数据库
grant all on Digital_school.* to 'test1'@'%' identified by 'test123' ;
flush privileges;刷新MySQL的系统权限
/****数据库连接超时****/
##### (2003, "Can't connect to MySQL server on '192.168.2.101' (timed out)") ####
1、----检查防火墙是否放行或者直接关闭防火墙
systemctl status firewalld.service # 查看防火墙状态
firewall-cmd --list-all # 查看防火墙状态
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33 ens36
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
systemctl stop firewalld.service # 关闭防火墙
2、----检查connect中的参数
connect常用参数:
hostname 连接的目标主机
port=SSH_PORT 指定端口
username=None 验证的用户名
password=None 验证的用户密码
database=None 指定数据库