This tutorial will guide you through the process of installing and configuring MySQL. You can choose between a local installation or using Docker. Detailed steps are provided for both methods.
$ sudo apt install mysql-server$ sudo service mysql status-
Open the MySQL configuration file for editing:
$ sudo nano /etc/mysql/my.cnf
-
Add the following line under the
[mysqld]section:[mysqld] skip-grant-tables
-
Restart the MySQL service:
$ sudo service mysql restart
-
Access MySQL as root:
$ mysql -u root
-
Switch to the MySQL database:
mysql> USE mysql; -
Update the root user's authentication string:
mysql> UPDATE user SET authentication_string='' WHERE User='root';
-
Flush privileges:
mysql> FLUSH PRIVILEGES; -
Exit MySQL:
mysql> EXIT;
-
Open the MySQL configuration file for editing:
$ sudo nano /etc/mysql/my.cnf
-
Remove or comment out the line added in Step 3:
# [mysqld] # skip-grant-tables
$ sudo service mysql restart$ mysql -u root -p- When prompted for a password, just press Enter.
mysql> (Type all commands in setup.sql)This tutorial provides step-by-step instructions for installing and configuring MySQL on a Windows system.
- Go to the official MySQL downloads page.
- Download the
MySQL Installerfor Windows.
- Run the downloaded
mysql-installer-web-community-<version>.exefile. - Follow the installation wizard:
- Choose the setup type. For most users, "Developer Default" is recommended.
- Click "Next" and then "Execute" to install the required components.
- After installation, the MySQL Installer will launch the configuration wizard.
- Configure the following:
- Config Type: Choose "Development Machine".
- Connectivity: Make sure the port is set to
3306. - Authentication Method: Choose "Use Strong Password Encryption".
- Set the root password and create additional user accounts if necessary.
- Complete the configuration steps and start the MySQL Server.
-
Open the Command Prompt.
-
Check the MySQL service status:
sc query MySQL
-
Log in to MySQL using the command line:
mysql -u root -p
-
Enter the root password when prompted.
-
Run the MySQL secure installation script:
mysql_secure_installation
-
Follow the prompts to improve the security of your MySQL installation:
- Set a root password.
- Remove anonymous users.
- Disallow root login remotely.
- Remove test database and access to it.
- Reload privilege tables.
-
Log in to MySQL:
mysql -u root -p
-
Create a new database:
CREATE DATABASE todolist;
-
Create a new user and grant privileges:
CREATE USER 'todolist'@'localhost' IDENTIFIED BY 'Todolist<123456789'; GRANT ALL PRIVILEGES ON todolist.* TO 'todolist'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;
Create a Dockerfile with the following content:
# Use the official MySQL image from Docker Hub
FROM mysql:latest
# Set the root password for MySQL
ENV MYSQL_ROOT_PASSWORD=password
# Expose port 3306 for external communication
EXPOSE 3306
# Start MySQL service
CMD ["mysqld", "--bind-address=0.0.0.0"]$ docker build -t mysql_server .$ docker run -d -p <PORT>:3306 --name mysql_container mysql_server
# Replace <PORT> with the desired port number$ docker ps -a$ docker exec -it CONTAINER_ID_OR_NAME mysql -uroot -p
# Password: password
mysql> (Copy all commands in setup.sql)-
Replace 'localhost' with '%' in MySQL Docker configuration because the Docker container runs on a different IP than 127.0.0.1.
-
If you accidentally execute the
setup.sqlscript with 'localhost', undo with the following commands:mysql> UPDATE mysql.user SET Host='%' WHERE User='todolist'; mysql> REVOKE ALL PRIVILEGES ON todolist.* FROM 'todolist'@'localhost'; mysql> GRANT ALL PRIVILEGES ON todolist.* TO 'todolist'@'%' WITH GRANT OPTION; mysql> FLUSH PRIVILEGES; mysql> -- Verify mysql> SELECT Host, User FROM mysql.db WHERE Db='todolist';
Create a Python script to test the connection:
import mysql.connector
# Define the connection parameters
host = 'localhost'
port = 3306 # This is the port mapped to MySQL in the Docker container
user = 'todolist'
password = 'Todolist<123456789' # Change this to your MySQL root password
try:
# Connect to the MySQL server
connection = mysql.connector.connect(
host=host,
port=port,
user=user,
password=password,
database='todolist'
)
if connection.is_connected():
print("Successfully connected to MySQL server")
# Get the MySQL server version
cursor = connection.cursor()
cursor.execute("SELECT VERSION()")
version = cursor.fetchone()[0]
print("MySQL server version:", version)
# Close cursor and connection
cursor.close()
connection.close()
print("Connection closed")
except mysql.connector.Error as error:
print("Failed to connect to MySQL server:", error)