Install and Configure MySQL on a Linux Server
Step 1: Update Your System
Open a terminal. Run these commands:
sudo apt update
sudo apt upgrade
Step 2: Install MySQL
Install MySQL server with:
sudo apt install mysql-server
Step 3: Secure MySQL Installation
Run the security script:
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow root login remotely, remove the test database, and reload privilege tables.
Step 4: Start MySQL Service
Ensure MySQL is running:
sudo systemctl start mysql
sudo systemctl enable mysql
Step 5: Log into MySQL
Access the MySQL shell:
sudo mysql -u root -p
Enter your root password when prompted.
Step 6: Create a New Database
In the MySQL shell, create a database:
CREATE DATABASE mydatabase;
Step 7: Create a New User
Create a user and grant privileges:
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
Step 8: Exit MySQL
Type:
EXIT;
Step 9: Configure Remote Access (Optional)
To allow remote access, edit the MySQL configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Find the line:
bind-address = 127.0.0.1
Change it to:
bind-address = 0.0.0.0
Restart MySQL:
sudo systemctl restart mysql
Step 10: Adjust Firewall (Optional)
If using UFW, allow MySQL:
sudo ufw allow mysql
MySQL is now installed and configured on your Linux server.
Sami Rahimi
Innovate relentlessly. Shape the future..
Recent Comments