Getting started with MySQL
From Crashcourse Wiki
Contents |
Overview
This page describes the most fundamental steps in installing and starting to use MySQL, step by step, on a Linux system. The instructions here are Fedora-centric but it shouldn't be hard to translate those few Fedora-specific commands to fit the distro of your choice.
In addition to the basic steps involved in getting MySQL up and running, these sections explain what you should look for after each step to verify that things seem to be progressing nicely. If, at any point, you're starting to see something different, you might want to stop to figure out why.
Clearing the decks
If you truly want to see what happens when you install and configure MySQL from scratch, you should verify that there are no leftovers from a previous installation. Try the following commands to remove any traces (feel free to check those locations first to see if any of it still exists):
# killall mysqld # userdel mysql # rm -rf /var/lib/mysql/ # rm -f /var/log/mysqld.log # rm -rf /var/run/mysqld/
In addition to any of the above, a previous installation of MySQL might have left an entry for "mysql" in the /etc/aliases file. It's not critical that you clear out all of that historical cruft, but it does make it easier to see what happens at each step.
The packages
On Fedora, installing MySQL will involve installing the following four packages:
- mysql
- mysql-server
- mysql-libs
- perl-DBD-MySQL
As it turns out, based on package dependencies, you simply need to do the following:
# yum install mysql-server
and the remaining packages come along for the ride.
The post-installation checklist
Immediately after the installation, and before starting mysqld, you can satisfy yourself that the installation worked properly by checking the locations that should have changed thusly:
# grep -w mysql /etc/passwd /etc/group [new "mysql" user and group] # ls /var/lib/mysql/ [should exist but be empty] # ls -l /var/log/mysqld.log [should be a zero-length file]
(As an aside, you can see what a package installation should do by checking its associated scripts with :
# rpm -q --scripts mysql-server
where you can see the above steps represented in the "preinstall" and "postinstall" scriptlets. If any of those scriptlet steps don't seem to have worked, you might want to take a closer look to see why not.)
Starting the mysqld server
On Fedora, simply run:
# service mysqld start
after which you can once again confirm that things seem to be functioning properly:
# service mysqld status # ls /var/lib/mysql/ [should show "mysql" and "test" DBs] # cat /var/log/mysqld.log
If all that looks good, you theoretically have a working MySQL installation.
Your first MySQL session
By way of getting your feet wet, you should now be able to do the following just to see the initial MySQL setup and examine the contents of those first two databases:
$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.45 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
At this point, try some of the following commands:
mysql> show databases; mysql> show tables from mysql; mysql> show tables from test; [should be empty] mysql> show columns from user from mysql; mysql> show columns from mysql.user; [equivalent] mysql> select user,host from mysql.user;
And, finally, you really should give the MySQL root account a password:
mysql> set password for 'root'@'localhost' = password('whatever');

