Low Orbit Flux Logo 2 F

How To Create Database In MySQL Using Python

Make sure you have the connector installed. This is assuming that you are using pip. You might do this differently if you are using conda or something else.



pip install mysql-connector-python

This is an example Python script that will create a new database in MySQL. Make sure that you substitute in your own parameters for your database.



import mysql.connector

host = "localhost"
user = "user1"
password = "secret_password1"
db_name = "new_special_database"

conn = mysql.connector.connect(
    host=host,
    user=user,
    password=password
)

db_query = f"CREATE DATABASE {db_name}"

cursor = conn.cursor()
cursor.execute(db_query)

cursor.close()
conn.close()