This Python program connects to a MySQL database to store and manage IP addresses. It includes basic functionality for creating a database connection, creating a table, and inserting IP addresses into the table. Logging is implemented to track successful operations and errors. The log file is created after you run the script.
- Connect to a My SQL database
- Create a table to store IP addresses
- Insert IP addresses into the table
- Log operations and errors
- Python 3 (Most recent version)
- MySQL Server (Most recent version)
- python -m venv venv
- venv\Scripts\activate # For Windows
- source venv/bin/activate # For maxOS/Linux
- python db_operations.py
-
Create a Database:
Open MySQL Workbench or another MySQL client and run:
CREATE DATABASE ip_addresses;
-
Create a new user:(Optional)
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'NewPassword';
-
Grant all privileges to the new user:
GRANT ALL PRIVILEGES ON . TO 'new_user'@'localhost' WITH GRANT OPTION;
-
Check Existing Users
SELECT User, Host FROM mysql.user WHERE User = 'root';
-
Modify Existing User
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword!';
-
Drop and Recreate User(be careful with this, it can affect permissions)
DROP USER 'root'@'localhost';