Connecting to the MySQL Server

The machine where MySQL runs and stores the data is called the MySQL server. To connect to the server, you have several setup options. First, you can have the MySQL client and MySQL server on your desktop, as shown in Figure 1.1. Second, you can have the MySQL client set up on your desktop while the MySQL server is on another machine that you connect to, as shown in Figure 1.2. Finally, your desktop can be any machine connecting to another machine with a MySQL client, which in turn connects to a MySQL server, either on the same machine or another, as shown in Figure 1.3.


Figure 1.1: Your machine has both the MySQL client and the MySQL server on it.


Figure 1.2: Your machine has the MySQL client. The MySQL server exists on another machine to which you connect.

click to expand
Figure 1.3: In this case, your terminal can be any machine capable of connecting to another, as it doesn't even run the lightweight MySQL client on it.

If the MySQL client is not on your desktop and you need to connect to a second machine to use the MySQL client, you'll probably use something such as Telnet or a Secure Shell (SSH) client to do so. Using one of these is a matter of opening the Telnet program, entering the hostname, username, and password. If you're unsure about this, ask your system administrator for help.

Once you've logged into a machine on which the MySQL client program is installed, connecting to the server is easy:

  • On a Unix machine (for example, Linux or FreeBSD), run the following command from the command line within your shell:

    % mysql -h hostname -u username -ppassword databasename
  • On a Windows machine, run the same command from the command prompt:

    % mysql -h hostname -u username -ppassword databasename

The % refers to the shell prompt. It'll probably look different on your machine—for example, c:\> on some Windows setups or $ on some Unix shells. The -h and the -u can be followed by a space (you can also leave out the space), but the -p must be followed by the password immediately, with no intermediate spaces.

Once you've connected, you'll encounter the mysql> prompt, which appears in most distributions on the command line once you've connected. You don't need to type it in; it will appear automatically. Even if a slightly different prompt appears, don't worry, just enter the text in bold. This is the convention used throughout the book.

Tip 

There is a more secure way of entering the password, which I recommend in a multiuser environment. Just enter the -p and omit the password. You'll be prompted for it when MySQL starts; then you can enter the password without it appearing on the screen. This avoids anyone seeing your password entered in plain text.

The hostname would be the machine hosting the server (perhaps something such as www.sybex.com or an IP such as 196.30.168.20). You don't need a hostname if you're already logged into the server (in other words, the MySQL client and server are on the same machine). The administrator assigns you the username and password (this is your MySQL password and username, which is not the same as your login to the client machine). Some insecure systems don't require any username or password.

Tip 

Sometimes the system administrator makes your life a little harder by not putting MySQL into the default path. So, when you type the mysql command, you may get a command not found error (Unix) or a bad command or file name error (Windows) even though you know you have MySQL installed. If this happens, you'll need to enter the full path to the MySQL client ( for example, /usr/local/build/mysql/bin/mysql, or on Windows, something such as C:\mysql\bin\mysql). Ask your administrator for the correct path if this is a problem in your setup.

To disconnect, simply type QUIT as shown:

mysql> QUIT Bye

You can also type EXIT or press Ctrl+D.

Note 

MySQL does not distinguish between uppercase and lowercase here. You could have typed QUIT, quit, or even qUIt if you'd wanted.



Mastering MySQL 4
Mastering MySQL 4
ISBN: 0782141625
EAN: 2147483647
Year: 2003
Pages: 230
Authors: Ian Gilfillan

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net