Skip to content

Setting up MySQL

Neil Cook edited this page Nov 9, 2020 · 2 revisions

Setting up MySQL

Installing MySQL

Following guide here

Install MySQL by the following command:

>>> sudo apt-get install mysql-server

The MySQL server is started automatically after installation. You can check the status of the MySQL server with the following command:

>>> sudo service mysql status

Stop the MySQL server with the following command:

>>> sudo service mysql stop

To restart the MySQL server, use the following command:

>>> sudo service mysql start

Configuring the Server

Set the root password

>>> sudo mysql_secure_installation

To test:

>>> sudo mysql -u root -p

To add user

>>> sudo mysql -u root -p
>>> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
>>> GRANT ALL PRIVILEGES ON *.* to 'newuser'@'localhost';
>>> FLUSH PRIVILEGES;

Connecting to and disconnecting from the server

From here

Connect via:

>>> mysql -h host -u user -p

Python interface

Install via pip

pip install mysql-connector-python

Using in python

import mysql.connector as mysql

db = mysql.connect(host='localhost', user='newuser', passwd='password')

Note: DO NOT install mysql-connector (it must be mysql-connector-python)