To run SQL commands in Linux, you must first install a database client (such as MySQL, PostgreSQL, or SQLite) and then execute the corresponding command-line interface (CLI) tool from your terminal to connect to the database server. Once connected, you can enter standard SQL queries to manage your data.

What You'll Need

  • A Linux distribution (such as Ubuntu, Debian, CentOS, or Fedora) installed and running.
  • Terminal access with sudo or root privileges for installation.
  • An active internet connection to download the database packages.
  • The credentials (username and password) for the database you intend to access.

Step-by-Step

  1. Install the Database Client: Open your terminal and install the client for the specific database you are using. Labels and package names may differ slightly by Linux distribution.
    • For MySQL/MariaDB: Run sudo apt install mysql-client (on Debian/Ubuntu) or sudo yum install mysql (on RHEL/CentOS).
    • For PostgreSQL: Run sudo apt install postgresql-client (on Debian/Ubuntu) or sudo yum install postgresql (on RHEL/CentOS).
    • For SQLite: Run sudo apt install sqlite3 (on Debian/Ubuntu) or sudo yum install sqlite (on RHEL/CentOS).
  2. Launch the Command Line Interface: Use the specific connection command for your database.
    • MySQL/MariaDB: Type mysql -u username -p. You will be prompted to enter your password. If the database is on a remote server, add -h hostname to the command.
    • PostgreSQL: Type psql -U username -d database_name. Depending on your configuration, you may be asked for a password.
    • SQLite: Type sqlite3 filename.db. Since SQLite is file-based, you simply point it to the database file.
  3. Verify the Connection: Once the prompt changes (e.g., showing mysql> or postgres=#), you are inside the SQL environment. Run a simple command to verify access, such as SELECT VERSION(); or .tables (for SQLite).
  4. Execute SQL Commands: Type your SQL queries. Remember that in most SQL command-line tools, a command is not executed until you add a semicolon (;) at the end of the statement and press Enter.
    • Example: SELECT * FROM users LIMIT 10;
  5. Exit the SQL Interface: When finished, use the exit command to return to the standard Linux bash prompt.
    • MySQL/MariaDB: Type exit; or quit;.
    • PostgreSQL: Type \q and press Enter.
    • SQLite: Type .exit or .quit.

Common Mistakes

  • Forgetting the Semicolon: The most common error is typing a full SQL statement and pressing Enter, only to see a blank line or a continuation prompt. SQL CLI tools require a ; to signal the end of the command.
  • Incorrect User Permissions: Trying to log in as a standard Linux user when the database requires a specific database user. Ensure you are using the -u or -U flag correctly.
  • Wrong Port or Host: Attempting to connect to a remote database without specifying the host (-h) or the port, causing the client to look for a local instance that isn't there.
  • Case Sensitivity: While SQL keywords (like SELECT) are case-insensitive, table names and column names may be case-sensitive depending on the Linux filesystem and database settings.

Tips

  • Use a Text Editor: For long, complex queries, write your SQL in a text editor (like Nano or Vim) first, then copy and paste it into the terminal to avoid typing errors.
  • Run Scripts Directly: Instead of typing commands manually, you can run an entire .sql file from the Linux bash prompt. For example: mysql -u user -p database_name < script.sql.
  • Use Tab Completion: Many modern SQL CLIs support tab completion for table and column names to speed up query writing.
  • Clear the Screen: In some clients, you can use Ctrl+L to clear the terminal screen while remaining inside the SQL prompt.

Troubleshooting

If you cannot connect to the database, follow these steps in order:

  1. Check Service Status: Ensure the database server is actually running. Use sudo systemctl status mysql or sudo systemctl status postgresql. If it is stopped, start it with sudo systemctl start [service].
  2. Verify Network Connectivity: If the database is remote, try to ping the server using ping [hostname] to ensure the server is reachable.
  3. Check Firewall Settings: Ensure the database port (e.g., 3306 for MySQL, 5432 for PostgreSQL) is open in the Linux firewall using ufw status or iptables -L.
  4. Validate Credentials: Double-check that the username and password are correct. If you have forgotten the root password, you may need to restart the database in safe mode to reset it.
  5. Check Socket Errors: If you see a "Can't connect to local MySQL server through socket" error, ensure the MySQL service is running and that you are pointing to the correct socket file path.

Frequently Asked Questions

Can I run SQL commands without installing a full server?

Yes. SQLite is a serverless database engine. You only need to install the sqlite3 client to create and manage database files directly on your Linux filesystem.

How do I run a command as the root user?

For MySQL/MariaDB, use sudo mysql -u root -p. For PostgreSQL, the default administrative user is usually postgres, so you can use sudo -u postgres psql.

Is there a way to see a list of all databases?

Yes. In MySQL/MariaDB, use SHOW DATABASES;. In PostgreSQL, use \l. In SQLite, since one file equals one database, use .databases.

How do I import a large SQL dump file?

Use the command line redirection operator. For MySQL, use mysql -u username -p database_name < backup.sql. This is much faster than copying and pasting the content into the prompt.

What is the difference between psql and mysql commands?

They are different CLI tools designed for different database engines. psql is the interactive terminal for PostgreSQL, while mysql is the tool for MySQL and MariaDB. They use different flags and "slash commands" for administrative tasks.