I have a Raspberry Pi and did the following steps:
Update the operating system:
sudo apt-get update && sudo apt-get upgrade
Install mySQL server:
sudo apt-get install mysql-server
Install mySQL client:
sudo apt-get install mysql-client
Check status of mySQL:
sudo service mysql status
Install mySQL python connector:
sudo apt-get -y install python3-mysql.connector
Give mySQL a password:
sudo mysql -uroot
use mysql;
update user set authentication_string=PASSWORD('mynewpassword') where User='root';
Note: In the above command keep the root user, just set the password.
Exit mySQL:
\q
Stop mySQL services:
sudo /etc/init.d/mysql stop
Start mySQL services:
sudo /etc/init.d/mysql start
Test the new password by logging in to the database:
sudo mysql -u root -p
SHOW DATABASES;
CREATE DATABASE YOUR_DATABASE_NAME_HERE;
CREATE USER 'YOUR_NEW_USERNAME'@'localhost' IDENTIFIED BY 'YOUR_NEW_PASSWORD';
Note: Python needs a new user to allow communication to mySQL! I have no idea why but it works.
In Python:
import mysql.connector
# The following code tests Pythons connection to mySQL
mydb = mysql.connector.connect(
host="localhost",
user="YOUR_NEW_USER",
password="YOUR_NEW_PASSWORD"
)
print(mydb)
It took me several days to piece this together from a dozen different websites. Hopefully my notes didn't miss anything. Creating the new user at the end seems to be the key issue. Mine isn't a Windows installation but the installation should be similar.