IT
파이썬 MYSQL 로그인 예제
carnival6103
2025. 1. 30. 22:15
반응형
파이썬과 MySQL을 이용한 로그인 시스템을 구현하는 방법을 단계별로 설명해드릴게요.
이 예제에서는 MySQL 데이터베이스에 사용자 정보를 저장하고, 파이썬을 통해 로그인 기능을 구현합니다.
1. MySQL 데이터베이스 설정
먼저 MySQL 데이터베이스에 사용자 정보를 저장할 테이블을 생성합니다.
CREATE DATABASE user_db;
USE user_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
2. 파이썬 환경 설정
파이썬에서 MySQL 데이터베이스에 연결하기 위해 mysql-connector-python 라이브러리를 설치합니다.
pip install mysql-connector-python
3. 사용자 등록 코드
사용자 정보를 데이터베이스에 저장하는 코드를 작성합니다.
import mysql.connector
from mysql.connector import Error
import hashlib
def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="user_db"
)
print("MySQL Database connection successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def register_user(username, password):
connection = create_connection()
cursor = connection.cursor()
hashed_password = hashlib.sha256(password.encode()).hexdigest()
query = "INSERT INTO users (username, password) VALUES (%s, %s)"
cursor.execute(query, (username, hashed_password))
connection.commit()
print("User registered successfully")
# 사용자 등록 예제
register_user("test_user", "test_password")
4. 로그인 기능 구현
사용자 로그인 기능을 구현하는 코드를 작성합니다.
def login_user(username, password):
connection = create_connection()
cursor = connection.cursor()
hashed_password = hashlib.sha256(password.encode()).hexdigest()
query = "SELECT * FROM users WHERE username = %s AND password = %s"
cursor.execute(query, (username, hashed_password))
user = cursor.fetchone()
if user:
print("Login successful")
else:
print("Invalid username or password")
# 로그인 예제
login_user("test_user", "test_password")
5. 전체 코드
위의 모든 코드를 하나로 합쳐서 전체 코드를 작성합니다.
import mysql.connector
from mysql.connector import Error
import hashlib
def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="user_db"
)
print("MySQL Database connection successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def register_user(username, password):
connection = create_connection()
cursor = connection.cursor()
hashed_password = hashlib.sha256(password.encode()).hexdigest()
query = "INSERT INTO users (username, password) VALUES (%s, %s)"
cursor.execute(query, (username, hashed_password))
connection.commit()
print("User registered successfully")
def login_user(username, password):
connection = create_connection()
cursor = connection.cursor()
hashed_password = hashlib.sha256(password.encode()).hexdigest()
query = "SELECT * FROM users WHERE username = %s AND password = %s"
cursor.execute(query, (username, hashed_password))
user = cursor.fetchone()
if user:
print("Login successful")
else:
print("Invalid username or password")
# 사용자 등록 예제
register_user("test_user", "test_password")
# 로그인 예제
login_user("test_user", "test_password")
이 예제는 기본적인 사용자 등록 및 로그인 기능을 구현한 것입니다. 실제 프로젝트에서는 보안 강화를 위해 추가적인 조치가 필요할 수 있습니다.
반응형