3.8 Exercises
Question 1:
If you connect to a local server on a Unix machine using the hostname localhost, will the connection be made using TCP/IP or a Unix socket file? How will the connection be made if you use the local host's actual name?
Question 2:
Which connection parameters identify you to the MySQL server?
Question 3:
You want to execute a number of prewritten queries using the MySQL server running on the host db.myexample.com. Your mysql username is juan and you want to be prompted for your password. The prewritten queries are stored in the file /tmp/queries.sql and you want world to be the default database. What command do you issue?
Question 4:
Having connected to a server with the mysql client program, you want to run a query and display its output vertically. What statement terminator do you use to end the query?
Question 5:
Using mysql, you're about to enter a record into a table that has only one column. You've typed INSERT INTO tbl VALUES ('teststring and pressed Enter. Now the prompt looks like this: '>. What do you enter to perform the following operations?
-
Send a valid query to the server
-
Cancel the query
Question 6:
In an interactive session using mysql, you want to insert records into a table in the test database using a text file containing INSERT statements. The file is named tbl_import.sql and resides in /tmp. What command do you use to process the file in the following ways?
-
Within the mysql session
-
From the shell of your operating system
Question 7:
You're in the process of entering a long SQL statement in mysql and you decide not to send it to the server. What do you type to cancel the statement?
Question 8:
How do you back up the tables City and Country from the world database into the file /tmp/world_backup.sql using a single mysqldump command?
Question 9:
How do you back up the databases db1 and db2 into the file /tmp/db1_db2_backup.sql with a single mysqldump command that ensures existing tables will be overwritten when you restore them?
Question 10:
What are the advantages and disadvantages of the multiple-row INSERT statements mysqldump can produce?
Question 11:
You want to produce a backup of the test database into the file /backups/structure.sql using mysqldump. However, the purpose of the backup file is to create an empty copy of each table on another server. What command do you issue?
Question 12:
How do you restore a backup of tables in the test database from a backup file called /backups/back.sql? What options can you use with mysqldump to ensure existing tables will be overwritten when the tables are restored? If you don't use this mysqldump option, what option can you use when you restore the tables to ensure that the restore operation doesn't stop when it finds that a table already exists in the database?
Question 13:
Describe the main difference between mysqlcheck and myisamchk.
Question 14:
For which table types can you use mysqlcheck?
Question 15:
For which table types can you use myisamchk?
Question 16:
In addition to providing access to your database, MySQLCC serves as a graphical tool for server administration.
-
What administrative actions does MySQLCC enable you to perform?
-
What types of information about the server does MySQLCC provide?
-
Besides the server, what else can you administer with MySQLCC?
Answers to Exercises
Answer 1:
-
A Unix socket file will be used.
-
When using the actual hostname, TCP/IP will be used.
Answer 2:
--user (or -u) and --password (or -p).
Answer 3:
shell> mysql -h db.example.com -p -u juan world < /tmp/queries.sql
Answer 4:
Use the \G sequence to display query output in vertical format.
Answer 5:
-
One possibility is ');
-
Another is to use the clear-query sequence: '\c
The '> prompt indicates that you began a single-quoted string but haven't finished it. Thus, you must enter the terminating single quote to finish the string regardless of whether you then want to enter the rest of the query or enter the cancel-query command.
Answer 6:
-
To process the file within the mysql session, use the SOURCE command:
-
To process the file from the shell of the operating system, invoke mysql and direct it to read from the file:
mysql> SOURCE /tmp/tbl_import.sql;
(Note that the semicolon is optional for the SOURCE command.) If test isn't the default database, you must first issue a USE test statement before processing the file.
shell> mysql test < /tmp/tbl_import.sql
You might also have to specify hostname, username, and password.
Answer 7:
To cancel a statement that you are entering, use the \c sequence.
Answer 8:
shell> mysqldump world City Country > /tmp/world_backup.sql
You might also have to specify hostname, username, and password.
Answer 9:
shell> mysqldump --add-drop-table --databases db1 db2 > /tmp/db1_db2_backup.sql
You might also have to specify hostname, username, and password.
Answer 10:
Advantages: Multiple-row statements can be reloaded more efficiently.
Disadvantages: Multiple-row statements are less readable and less easily ported to other database management systems.
Answer 11:
shell> mysqldump --no-data test > /backups/structure.sql
You might also have to specify hostname, username, and password.
Answer 12:
To restore tables, you can use this command:
shell> mysql test < /backups/back.sql
You might also have to specify hostname, username, and password.
To ensure that existing tables will be overwritten for the restore operation, you can use this command when dumping the tables:
shell> mysqldump --add-drop-table test > /backups/back.sql
To restore backups made without the --add-drop-table option, you can use the mysql --force, which will continue processing queries in batch mode even if errors occur.
Answer 13:
The main difference between mysqlcheck and myisamchk lies in how they access tables:
-
mysqlcheck is a client program; it determines which options were given on the command line, and then sends appropriate SQL statements to the MySQL server to perform the requested operation.
-
myisamchk is not a client program for the server. It performs operations on tables by accessing the table files directly.
Answer 14:
mysqlcheck can perform operations on MyISAM, InnoDB, and BDB tables.
Answer 15:
myisamchk works only for MyISAM tables.
Answer 16:
-
MySQLCC enables you to kill client connections, issue flush commands, and shut down the server.
-
MySQLCC has a server information window that provides single-click access to the same system and status variables that you get when issuing SHOW VARIABLES and SHOW STATUS statements manually.
-
MySQLCC also enables you to administer user accounts.