How to Send Sql Queries to Mysql from the Command Line

Find the mysql program (Should be in a subdirectory called bin under the directory where MySQL was installed) E.g., Start mysql - At the command prompt, type: mysql -h hostname -u username -p, where host is the machine where the MySQL server is...

13 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Find the mysql program (Should be in a subdirectory called bin under the directory where MySQL was installed) E.g.

    Windows users:
    C:\mysql\bin\mysql.exe E.g.

    Linux/Unix users: /usr/local/mysql/bin/mysql ,,, The response from the server should be displayed on your screen. ,, Windows users:
    C:\mysql\bin\mysql.exe E.g.

    Linux/Unix users: /usr/local/mysql/bin/mysql
  2. Step 2: Start mysql - At the command prompt

    db_name is the name of the database to run the query in, and, query is the query that you want to run. ,,
  3. Step 3: type: mysql -h hostname -u username -p

  4. Step 4: where host is the machine where the MySQL server is running username is the MySQL account you want to use -p will make mysql prompt you for the MySQL account password.

  5. Step 5: Enter your password when prompted.

  6. Step 6: Type your SQL command followed by a semi-colon (;) and press the Enter key.

  7. Step 7: To get out of mysql

  8. Step 8: type quit at the prompt and press the Enter key.

  9. Step 9: Find the mysql program (Should be in a subdirectory called bin under the directory where MySQL was installed) E.g.

  10. Step 10: Start mysql - At the command prompt

  11. Step 11: type: mysql -h hostname -u username -p db_name -e "query" where host is the machine where the MySQL server is running username is the MySQL account you want to use -p will make mysql prompt you for the MySQL account password.

  12. Step 12: Enter your password when prompted.

  13. Step 13: MySQL should return the result of your query.

Detailed Guide

Windows users:
C:\mysql\bin\mysql.exe E.g.

Linux/Unix users: /usr/local/mysql/bin/mysql ,,, The response from the server should be displayed on your screen. ,, Windows users:
C:\mysql\bin\mysql.exe E.g.

Linux/Unix users: /usr/local/mysql/bin/mysql

db_name is the name of the database to run the query in, and, query is the query that you want to run. ,,

About the Author

N

Nicole Hughes

Committed to making practical skills accessible and understandable for everyone.

71 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: