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...
Step-by-Step Guide
-
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 -
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. ,, -
Step 3: type: mysql -h hostname -u username -p
-
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.
-
Step 5: Enter your password when prompted.
-
Step 6: Type your SQL command followed by a semi-colon (;) and press the Enter key.
-
Step 7: To get out of mysql
-
Step 8: type quit at the prompt and press the Enter key.
-
Step 9: Find the mysql program (Should be in a subdirectory called bin under the directory where MySQL was installed) E.g.
-
Step 10: Start mysql - At the command prompt
-
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.
-
Step 12: Enter your password when prompted.
-
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
Nicole Hughes
Committed to making practical skills accessible and understandable for everyone.
Rate This Guide
How helpful was this guide? Click to rate: