Getting Started with MySQL: Installation and Basic Operations


MySQLStart by updating your package repository and installing MySQL server on a Linux system:
sudo apt update
sudo apt install mysql-serverMySQL & Create a UserTo access MySQL, you can use the MySQL command-line tool. If you haven't set up a specific user yet, you can access it as the root user:
sudo mysqlAlternatively, if you have set up a MySQL user, you can access MySQL with:
mysql -u username -pReplace username with your actual MySQL username.
Inside the MySQL shell, you can create a new user by executing:
CREATE USER 'dbUsername'@'localhost' IDENTIFIED BY 'password';CREATE USER: This statement is used to create a new MySQL user.
'dbUsername'@’localhost’: Specifies the username and the hostname. Replace dbUsername with your desired username. The localhost indicates that this user can only connect from the local machine. Use % to allow connections from any host.
IDENTIFIED BY 'password': Sets the password for the user. Replace password with your desired password.
After creating the user, you might need to grant them necessary permissions:
GRANT ALL PRIVILEGES ON *.* TO 'dbUsername'@'localhost' WITH GRANT OPTION;This grants all privileges to the user on all databases and tables.
To create a new database, use the following command:
CREATE DATABASE tutorial_database;To verify the creation of your database, list all databases:
SHOW DATABASES;Navigate to the directory containing your .sql file. Then, use the following command to import it into your database:
mysql -u username -p database_name < file.sqlReplace username, database_name, and file.sql with your MySQL username, the name of your database, and the .sql file name, respectively.
Once logged into MySQL, you can manage your databases. List all databases:
SHOW DATABASES;
USE your_database_name;
SHOW TABLES;
DESCRIBE table_name;
SELECT * FROM table_name;MySQL ServiceTo apply changes, you may need to restart the MySQL service:
sudo systemctl restart mysqlCheck the status of the MySQL service to ensure it's running:
sudo systemctl status mysqlAlternatively, you can check if MySQL is listening for connections:
sudo ss -tulnp | grep mysqldTo delete a database, use the DROP DATABASE command:
DROP DATABASE helios;Replace
database_namewith the name of the database you wish to delete.
By following these steps, you should be able to manage MySQL databases effectively on your server. Remember to replace placeholder values with your actual database names, usernames, and passwords as appropriate.