MySQL has a useful command line tool which can be used to query and manage databases and users. This command is simply "mysql" and will usually be in the command path on Linux and BSD distributions, although to use it on Windows you would normally first need to change to the directory/folder that the MySQL binary applications are before running "mysql". This post gives a brief overview of logging in and a couple of commands for listing and changing databases, and running queries.
Logging in
The basic usage of the "mysql" command is this:
The -u flag allows you to specify the username to log in as, replacing [username] in the example above with your username, and the -p flag that you want to enter a password. You can omit these but after starting up the command prompt you won't be able to do much. With the -p flag specified, you'll be prompted for a password before continuing. You can also tell the MySQL command line client to automatically select a particular database by specifying it at the end, e.g.:
After entering your password (successfully), the mysql command prompt will start and you'll see something like this:
You can quit from the MySQL command line client at any time by entering \q and then <enter>
Running queries
To run a query, enter your query, follow it with ; and then hit the enter key. For example:
Selecting/changing a database
You can change to a different database at any stage that you have the rights to access. If you passed one in as a command line argument then it will have been selected automatically, but you can change it at any stage too. To select a specific database, do this:
This will change to the specified database, if it exists. It it doesn't, you'll get an error like this:
Listing available databases
You can list the available databases by doing this:
This will show a list of databases which you can then change to using the "use" command detailed above. For example:
0 comments:
Post a Comment