Skip to content

Instantly share code, notes, and snippets.

@KirillY
Last active June 20, 2022 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KirillY/6831c230239c226403fbb5d14c32e226 to your computer and use it in GitHub Desktop.
Save KirillY/6831c230239c226403fbb5d14c32e226 to your computer and use it in GitHub Desktop.
#teachmeskills #mysql setup

Setup

Check if already installed

sudo service mysql start

If not installed, install

sudo apt update
sudo apt install mysql-server

Start server and enter sql console

sudo service mysql start
sudo mysql

Change password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'qwerty@123';
FLUSH PRIVILEGES;

Install mysql-connector-python

poetry add mysql-connector-python

Usage

Connect using CLI

 mysql --password=qwerty@123 --user=root

Connect using Python

import mysql.connector as mysql
db = mysql.connect(
    host="localhost",
    user="root",
    passwd="qwerty@123"
)

cursor = db.cursor()
cursor.execute("CREATE DATABASE test_db")
cursor.execute("SHOW DATABASES")
print(cursor.fetchall())
cursor.execute("DROP DATABASE test_db")
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment