This website collects cookies to deliver better user experience
MySQL database management using the terminal
MySQL database management using the terminal
To log in to MySQL server via terminal, use login credentials and hostname.
mysql -h localhost -u username -p
MySQL server will prompt for the password. The -h hostname flag is optional use only when the host is different from the default value localhost.
Once logged in, you can run any SQL query in the MySQL console. However, select the database first.
showdatabases;
The above command will display a list of all databases. Run the use command to select a database.
use database_name;
Now you can run any SELECT, UPDATE, DELETE query on the table. You can CREATE and DROP the tables.
To see the list of all tables, run this command.
showtables;
Describe table command shows the columns and their attributes like names, data types, collation, primary key, index nullability.
describe table_name;DESC table_name;
Similarly, describe user command displays user details and privileges.
describe user_name;
Things to remember in MySQL command-line
Before moving to the user and database creation command, there are a few things to remember.
Use a terminator; after every query.
For a multi-line query, hit enter to go to the new line.
To clear the SQL query without running it, use the clear \c flag.
To get more help, use the help \h flag.
To exit from MySQL, use the quit \q flag.
Root user VS non-root user
The MySQL root user has full access to the database server. Using root can seriously mess things up or delete critical stuff on the server without even warnings. It would be insecure to access MySQL as a root user from a website.
To create a non-root new MySQL user and give it only needed privileges and access to a database, follow these steps.