For Fedora, the MySQL server is off by default. To
To start the MySQL server immediately, type the following from a Terminal window as root
# service mysqld start
To set the MySQL server to start each time the computer reboots, type the following (as root):
# chkconfig mysqld on
This sets mysqld to start during most multiuser run states (levels 3, 4, and 5). To check that the service is turned on for those levels, type chkconfig --list mysqld from a Terminal window.
You can use the mysqladmin or mysqlshow commands to check that the MySQL server is up and running. Here’s an example of how to check information about the MySQL server using the mysqladmin command.
# mysqladmin -u root -p version proc Enter password: ******** mysqladmin Ver 8.23 Distrib 3.23.58, for redhat-linux-gnu on i386 Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL license Server version 3.23.58 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/lib/mysql/mysql.sock Uptime: 2 days 10 hours 47 min 35 sec Threads: 1 Questions: 184 Slow queries: 0 Opens: 1 Flush tables: 3 Open tables: 1 Queries per second avg: 0.001 +----+------+----------+------+-------+------+-----+------------------+ IdUser Host db Command Time StateInfo +----+------+----------+------+-------+------+-----+------------------+ 52 root localhost Query 0 show processlist +----+------+----------+------+-------+------+-----+------------------+
Each of the two options to
mysqladmin
shown here provides useful information. The version information shows the
mysqladmin
version is 8.23 and the number assigned to this distribution of the mysql server is 3.23.58. The binary package was created for PC versions of Linux/GNU on the i386 processor. The connection to the server is through a UNIX socket (
mysql.sock
) on the local host. The server has been up for 2 days, 10 hours, 47 minutes, and 35 seconds. Statistics show that there is one thread (connection to the server) currently active. There have been 184
The
proc
option shows that one client is currently connected to the server. That client is logged into MySQL as the root user on the
localhost
. The client that has an Id of 52 (which
If the server were not running at the moment, the mysqladmin command shown in the previous example would result in a failure message:
mysqladmin: connect to server at 'localhost' failed.
Recommended remedies are to try to restart the server (by typing service mysqld restart ) or to make sure that the socket exists ( /var/lib/mysql/mysql.sock ).
The first time you start the MySQL server (using the start-up script described previously), the system creates the initial grant tables for the MySQL database. It does this by running the mysql_install_db command.
The mysql_install_db command starts you off with two databases: mysql and test . As you create data for these databases, that information is stored in the /var/lib/mysql/mysql and /var/lib/mysql/test directories, respectively.
Because the MySQL root user doesn't have to be the system's root user (provided you have the MySQL root user's password), you can be logged into Fedora as any user you choose. In the following examples, be sure to provide the MySQL root
To get started creating databases and tables, you can use the
mysql
command. From any Terminal window,
$ mysql -u root -p mysql Enter password: ********* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 39 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer mysql>
Type in the root user’s MySQL password as prompted. (If no password has been set, you can skip the
-p
option.) The
mysql>
prompt appears, ready to accept commands for working with the mysql default database on the localhost. If you are connecting to the MySQL server from another host computer, add
-h
hostname
to the command line (where
hostname
is the
As the MySQL monitor welcome text notes, be sure to end each command that you type with a semicolon (
;
) or
\g
. If you type a command and it appears to be waiting for more input, it’s probably because you
Before you begin using the mysql interface to create databases, try checking the status of the MySQL server using the status command. The following is an example of output from the status command:
mysql> status -------------- mysql Ver 11.18 Distrib 3.23.58, for pc-linux-gnu (i686) Connection id: 43 Current database: mysql Current user: root@localhost Current pager:stdout Using outfile: '' Server version: 3.23.58 Protocol version: 10 Connection: Localhost via UNIX socket Client characterset: latin1 Server characterset: latin1 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 1 day 2 hours 57 min 19 sec Threads: 1 Questions: 136 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.001 --------------
The status information
Within an interactive
mysql
session, you can create and modify databases and tables. If you are not already connected to a
mysql
session, type the following command (
$ mysql -u root -p Enter password: ******* mysql>
The general steps for creating a MySQL database include creating the database name, identifying the new database as the current database, creating tables, and adding data to the tables. While you are connected to a mysql session, you can run the following procedure to create a sample database.
To create a new database name, use the CREATE DATABASE command at the mysql> prompt. For example, to create a database named allusers, type the following:
mysql> CREATE DATABASE allusers;
This action creates a database called allusers in the /var/lib/mysql directory. (While you don't have to use capitals for the commands just shown, it makes it easier to distinguish the commands from the database entries.)
| Note |
Alternatively, you could create a database from the command line using the mysqladmin command. For example, to create the database named allusers with mysqladmin , you could type the following: mysqladmin -u root -p create allusers |
To see what databases are available for your mysql server, type the following at the mysql> command prompt:
mysql> SHOW DATABASES; +----------+ Database +----------+ allusers mysql test +----------+ 3 rows in set (0.00 sec)
The databases shown here are named allusers , mysql , and test . The allusers database is the one created in the previous step. The mysql database contains user access data. The test database is created automatically for creating test mysql databases.
To work with the database you just created ( allusers ), you need to make allusers your current database. To do that, type the following at the mysql> command prompt:
mysql> USE allusers; Database changed
Creating a table for your database requires some planning and understanding of table syntax. You can type in the following commands and column information to try out creating a table. For more detailed information on creating tables and using different data types, refer to the section “Understanding MySQL Tables” later in this chapter.
To create a table called
mysql> CREATE TABLE names ( ->firstname varchar(20) not null, -> lastname varchar(20) not null, -> streetaddr varchar(30) not null, -> city varchar(20) not null, -> state varchar(20) not null, ->zipcode varchar(10) not null -> ); Query OK, 0 rows affected (0.00 sec)
You have now created a table called
names
for a database named
allusers
. It contains
With a database and one table created, you can now add data to the table.
After the database is created and the structure of a database table is in place, you can begin working with the database. You can add data to your MySQL database by manually entering each record during a mysql session or by adding the data into a plain-text file and loading that file into the database.
To do the procedure in this section, I assume you have an open interactive mysql session and that you have created a database and table as described in the previous section. If you are not already connected to a mysql session, type the following command (assuming the mysql user name of root):
$ mysql -u root -p Enter password: ******* mysql>
To add data to an existing MySQL database, the following procedure describes how to view the available tables and load data into those tables manually. The
To make the database you want to use your current database (in this case, allusers ), type the following command from the mysql> prompt:
mysql> USE allusers; Database changed
To see the tables that are associated with the current database, type the following command from the mysql > prompt:
mysql> SHOW tables; +--------------------+ Tables_in_allusers +--------------------+ names +--------------------+ 1 row in set (0.00 sec)
You can see that the only table defined so far for the allusers database is the one called names .
To display the format of the names table, type the following command at the mysql > prompt:
mysql> DESCRIBE names; +-----------+------------+------+-----+---------+-------+ Field Type Null Key Default Extra +-----------+------------+------+-----+---------+-------+ firstname varchar(20) lastname varchar(20) streetaddr varchar(30) city varchar(20) state varchar(20) zipcode varchar(10) +-----------+------------+------+-----+---------+-------+
To add data to the new table, you can use the INSERT INTO command from the mysql> prompt. Here is an example of how to add a person’s name and address to the new table:
mysql>
INSERT INTO names
->
VALUES ('Jerry','Wingnut','167 E Street',
->
'Roy','UT','84103');
In this example, the INSERT INTO command identifies the names table. Then it indicates that values for a record in that table include the name Jerry Wingnut at the address 167 E Street, Roy, UT 84103.
To check that the data has been properly entered into the new table, type the following command from the mysql> prompt:
mysql> SELECT * FROM names; +-----------+----------+-------------+-------+-------+---------+ firstname lastname streetaddr city state zipcode +-----------+----------+-------------+-------+-------+---------+ Jerry Wingnut 167 E Street Roy UT 84103 +-----------+----------+-------------+-------+-------+---------+
The resulting output shows the data you just entered, displayed in the columns you defined for the names table. If you like, you can continue adding data in this way.
Typing each data item individually can be
Using the LOAD DATA command during a mysql session, you can load a file containing database records into your MySQL database. Here are a few things you need to know about creating a data file to be loaded into MySQL.
You can create the file using any Linux text editor.
Each record, consisting of all the columns in the table, must be on its own line. (A line feed indicates the start of the next record.)
Separate each column by a Tab character.
You can leave a column blank for a particular record by placing a \N in that column.
Any blank lines you leave in the file result in blank lines in the database table.
In this example, the following text is added into a plain-text file. The text is in a format that can be loaded into the names table created earlier in this chapter. To try it out, type the following text into a file. Make sure that you insert a Tab character between each value.
Chris Smith 175 Harrison Street Gig Harbor WA 98999 John Jones 18 Talbot Road NW Coventry NJ 54889 Howard Manty 1515 Broadway New York NY 10028
When you are done entering the data, save the text to any filename that is accessible to the mysql server daemon (for example, /tmp/name.txt ). Remember the filename so that you can use it later. If you are not already connected to a mysql session, type the following command (assuming mysql is the user name root):
$ mysql -u root -p Enter password: ******* mysql>
Next, identify the database ( allusers in this example) as the current database by typing the following:
mysql> USE allusers; Database changed
To actually load the file into the names table in the allusers database, type the following command to load the file (in this case, /tmp/name.txt ) from the mysql> prompt.
| Note |
Either enter the full
|
mysql> LOAD DATA INFILE "/tmp/name.txt" INTO TABLE names; Query OK, 3 rows affected (0.02 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
Type the following at the mysql> prompt to make sure that the records have been added correctly:
mysql> SELECT * FROM names; +----------+---------+--------------------+------------+-------+-------+ firstname lastname streetaddr city state zipcode +----------+---------+--------------------+------------+-------+-------+ Chris Smith 175 Harrison Street Gig Harbor WA 98999 John Jones 18 Talbot Road NW Coventry NJ 54889 Howard Manty 1515 Broadway New York NY 10028 +----------+---------+--------------------+------------+-------+-------+