Logging On to MySQL - Page 2
January 10, 2002
The standard interface for working with MySQL databases is to
connect to the MySQL server software (which you set up in Chapter
1) and type commands one at a time. To make this connection to
the server, you'll need the MySQL client program. If you
installed the MySQL server software yourself either under Windows
or under some brand of UNIX, you already have this program
installed in the same location as the server program. Under
Linux, for example, the program is called
If you didn't set up the MySQL server yourself (if, for example, you'll be working on your Web host's MySQL server), there are two ways to connect to the MySQL server. The first is to use Telnet or a Secure Shell (SSH) connection to log into your Web host's server, and then run mysql from there. The second is to download and install the MySQL software from http://www.mysql.com/ (available free for Windows and Linux) on your own computer, and use it to connect to the MySQL server over the Internet. Both ways work fine, and your Web host may support one, the other, or both (you'll need to ask).
Whichever method and operating system you use, you'll end up at a command line, ready to run the MySQL client program and connect to your MySQL server. Here's what you should type:
You need to replace
If you typed everything properly, the MySQL client program will introduce itself and then dump you on the MySQL command line:
Now, the MySQL server can actually keep track of more than one database (this allows a Web host to set up a single MySQL server for several of its subscribers to use, for example), so your next step should be to pick a database to work with. First, let's retrieve a list of databases on the current server. Type this command (don't forget the semicolon!), and hit ENTER.
MySQL will show you a list of the databases on the server. If this is a brand new server (i.e. if you installed this server yourself in Chapter 1), the list should look like this:
The MySQL server uses the first database, called
If you type this command and press Enter, MySQL will obediently delete the database, saying "Query OK" in confirmation. Notice that you're not prompted with any kind of "are you sure?" message. You have to be very careful to type your commands correctly in MySQL because, as this example shows, you can obliterate your entire database—along with all the information it contains—with one single command!
Before we go any further, let's learn a couple of things about
the MySQL command line. As you may have noticed, all commands in
MySQL are terminated by a semicolon (
MySQL shows you that it's waiting for you to type more of your
command by changing the prompt from
If you get halfway through a command and realize you made a
mistake early on, you may want to cancel the current command
entirely and start over from scratch. To do this, type
MySQL will completely ignore the command you had begun to type, and will go back to the prompt to wait for another command.
Finally, if at any time you want to exit the MySQL client program, just type "quit" or "exit" (either one will work). This is the only command that doesn't need a semicolon, but you can use one if you want to.