Type: SELECT id, first, last FROM name ►► ORDER BY id; The window should look like this: In this query, ORDER BY id places the records in numeric order, based on their id numbers.. GE
Trang 1GETTING STARTED WITH MYSQL
38
3 Type:
SELECT id, first, last FROM name ►►
ORDER BY id;
The window should look like this:
In this query, ORDER BY id places the records in numeric order, based on their id numbers
Tip: To arrange records in reverse numeric or reverse
alphabetical order, add DESC on the end For instance, type:
SELECT first, last FROM name ORDER BY last
DESC;
The DESC option refers to the word “descending.” It tells MySQL
to order things descending from high to low instead of the default: low to high
Trang 2GETTING STARTED WITH MYSQL 39
4 Type:
\q;
then press ENTER
This closes your MySQL database connection
You are now logged out of the MySQL server: the mysql> prompt is gone
Trang 3GETTING STARTED WITH MYSQL
40
5 Type:
exit then press ENTER
The Konsole window should close
Trang 4GETTING STARTED WITH MYSQL 41
Giving MySQL commands to a Web server
MySQL’s client/server arrangement makes it well-suited to Web applications With MySQL server running on a Web server, you can use a MySQL client to update/add/delete data remotely
This book assumes that you’ve installed MySQL on your desktop Linux computer Both the MySQL client and server programs are
on this computer, called localhost
To give commands to a MySQL server program running on a Linux Web server, just replace localhost with the IP address
of the Web server, such as 10.0.1.10
or the domain name of the Web server, such as mysql.domain.com
Provided you have an Internet connection with the Web server, and the proper username/password to access it, your commands will work
Trang 5GETTING STARTED WITH MYSQL
42
Trang 6ADMINISTERING DATABASES 43
Administering
Databases
In this section, you’ll learn how to:
• Restart MySQL
• Back up a database
• Delete a table
• Delete a database
• Restore a database
Trang 7ADMINISTERING DATABASES
44
Restart MySQL
If you’ve shut off your computer since the last exercise, you might need to restart MySQL
First, login to your Linux computer as the Root user Then restart the MySQL server:
1 Open the Konsole window and login as the Root user
2 At the prompt, type:
/etc/rc.d/init.d/mysql start
then press ENTER
3 Type:
exit then press ENTER
You’re now logged out of the Linux Root account
Trang 8ADMINISTERING DATABASES 45
Now you’ll have to establish a MySQL client connection to the MySQL server:
4 At the $ prompt, type:
mysql –u root –p
then press ENTER
5 Type the password used to gain root access to the MySQL
server:
textbook
then press ENTER
Trang 9ADMINISTERING DATABASES
46
The window should look like this:
Trang 10
ADMINISTERING DATABASES 47
Back up a database
1 Make sure the Konsole window is open If it’s not, open it
2 Make sure you’re logged out of the MySQL server
Tip: Give the \q; command
3 At the[yourusername@localhost yourusername]$
prompt, type:
pwd
then press ENTER
The window should look something like this:
Trang 11ADMINISTERING DATABASES
48
The Linux command pwd is an acronym for print working directory In other words, “print the path to the directory I’m working in.”
When you first open the Konsole window, Linux automatically goes to your home directory
Each user on a Linux computer has his own home directory, which contains preferences and files unique to that user
Trang 12ADMINISTERING DATABASES 49
4 Type:
cd / then press ENTER
The window should look something like this:
This Linux command has two parts:
cd tells the computer to change directory
/ takes you to the root directory
Trang 13ADMINISTERING DATABASES
50
Root directory at the top
Starting from the Root directory, you can dig down into all the other directories, or folders, on the computer
Trang 14
ADMINISTERING DATABASES 51
5 Type:
pwd then press ENTER
The window should look something like this:
This is the path to your current working directory on this computer: root
6 Type:
cd then press ENTER
The cd command takes you to your home directory
Trang 15ADMINISTERING DATABASES
52
7 Type:
pwd then press ENTER
You should now be back where you started, in your home directory:
8 Type:
mkdir backups then press ENTER
mkdir is a Linux command to create a new directory, in this case a new directory within your home directory called backups
9 Type:
ls then press ENTER
The ls command lists all the items in the current directory: the backups, Desktop, Documents, and tmp directories