• Tidak ada hasil yang ditemukan

Connecting to the Server

Dalam dokumen Buku Learning MySQL and MariaDB (Halaman 59-62)

Once you know your MySQL username and password, you can connect to the MySQL server with the mysql client. For instance, I gave myself the username russell so I can connect as follows from a command line:

mysql -u russell -p

It’s useful to understand each element of the previous line. The -u option is followed by your username. Notice that the option and name are separated by a space. You would replace russell here with whatever username you’ve created for yourself. This is the MySQL user, not the user for the operating system. Incidentally, it’s not a good security practice to use the root user, unless you have a specific administrative task to perform for which only root has the needed privileges. So if you haven’t created another user for yourself, go back and do that now. To log into MariaDB, you would enter the same command and options as for MySQL.

The -p option instructs the mysql client to prompt you for the password. You could add the password to the end of the -p option (e.g., -pRover#My_1st_Dog&Not_Yours!, where the text after -p is the password). If you do this, leave no space between -p and the password.

However, entering the password on the command line is not a good security practice either, because it displays the password on the screen (which others standing behind you may see), and it transmits the password as clear text through the network, as well as

making it visible whenever someone gets a list of processes that are running on the server.

It’s better to give the -p option without the password and then enter the password when asked by the server. Then the password won’t be displayed on the screen or saved anywhere.

If you’re logged into the server filesystem with the same username as you created for MySQL, you won’t need the -u option; the -p is all you’ll need. You could then just enter this:

mysql -p

Once you’ve entered the proper mysql command to connect to the server, along with the password when prompted, you will be logged into MySQL or MariaDB through the client.

You will see something that looks like this:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1419341

Server version: 5.5.29 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

If MariaDB is installed on your server, you will see something like the following:

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 360511

Server version: 5.5.33a-MariaDB MariaDB Server, wsrep_23.7.6.rXXXX Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>>

The first line, after “Welcome to the MySQL/MariaDB monitor,” says that commands end

with a semicolon (;) or a slash-g (\g). When you enter a command, or rather an SQL statement, you can press Enter at any point to go to the next line and continue entering more text. Until you enter either ; or \g, the mysql client will not transmit what you’ve entered to the MySQL server. If you use \G, with an uppercase G, you’ll get a different format. We’ll cover that format later. For now, just use the semicolon.

The second line in the output shown tells you the identification number for your

connection to the server. One day you may get in trouble and need to know that. For now you can ignore it.

The third line tells you which version of MySQL or MariaDB is installed on the server.

That can be useful when you have problems and discover in reading the online

documentation that the problem is in a particular version, or when you want to upgrade the server but need to know which version you have now before upgrading.

The next line talks about getting online help. It provides help for all of the SQL statements and functions. Try entering these commands to see what the client returns:

help

This command provides help on using the mysql client.

help contents

This command shows you a list of categories for help on major aspects of MySQL or MariaDB. In that list, you will see one of the categories is called Data Manipulation.

These are SQL statements related to inserting, updating, and deleting data.

hep Data Manipulation

This command will display all of those statements for which help is available from the client. One of those SQL statements is SHOW DATABASES.

help SHOW DATABASES

This command shows how to retrieve the help information related to that SQL

statement. As you can see, there is plenty of useful information accessible within the client. If you can’t quite remember the syntax of an SQL statement, it’s a quick way to retrieve the information.

The first help command provides help on using the mysql client. The second help

command shows you a list of categories for help on major aspects of MySQL or MariaDB.

In that list, you will see one of the categories is called, Data Manipulation. These are SQL statements related to inserting, updating, and deleting data. The third help command will display all of those statements for which help is available from the client. One of those SQL statements is SHOW DATABASES. The last help command shows how to retrieve the help information related to that SQL statement. As you can see, there is plenty of useful information accessible within the client. If you can’t quite remember the syntax of an SQL statement, it’s a quick way to retrieve the information.

A minor but sometimes useful tip is included in the third line of the opening results: to cancel an SQL statement once you’ve started typing it, enter \c and press Enter without a closing semicolon. It will clear whatever you have been entering, even on previous lines, from the buffer of the mysql client, and return you to the mysql> prompt.

The very last line, the mysql>, is known as the prompt. It’s prompting you to enter a

command, and is where you’ll operate during most of this book. If you press Enter without finishing a command, the prompt will change to -> to indicate that the client hasn’t yet sent the SQL statement to the server. On MariaDB, the default prompt is different. It shows MariaDB [(none)]>> to start. When you later set the default database to be used, the none will be changed to the name of the current default database.

Incidentally, it is possible to change the prompt to something else. To do so, enter the client command prompt followed by the text you want to display for the prompt. There are a few special settings (e.g., \d for default database). Here’s how you might change the prompt:

prompt SQL Command \d>\_

And here’s how the prompt will look after you run the preceding command to change it:

SQL Command (none)>

Right now you have no default database. So now that you have the mysql client started, let’s start exploring databases.

Dalam dokumen Buku Learning MySQL and MariaDB (Halaman 59-62)