In this part of the MySQL tutorial, we are going to cover the installation of the MySQL database management system. In this chapter, we will install MySQL on Linux.
There are several ways how we can install MySQL on our system. We can install MySQL from packages, from binaries or from the sources.
$ sudo apt-get install mysql-server
On Ubuntu and other debian based distributions, we can easily install MySQL from packages by using the apt-get tool. This command installs the MySQL server and various other packages. While installing the packages, we are prompted to enter a password for the MySQL root account.
Installing from sources
Installing MySQL from sources gives us the most options to build MySQL according to our preferences. We can customize installation locations, various build parameters or compiler optimizations.
We enter the mysql.com website. Under Downloads tab, we select generally available MySQL Community Server. From the platform combo box, we select Source Code.
$ ls -sh mysql-5.5.9.tar.gz 23M mysql-5.5.9.tar.gz
We have downloaded a compressed archive of MySQL 5.5.9 sources.
$ tar xzvf mysql-5.5.9.tar.gz $ cd mysql-5.5.9/
We have unpacked the sources to the mysql-5.5.9 directory.
$ sudo apt-get install cmake $ which cmake bison perl /usr/bin/cmake /usr/bin/bison /usr/bin/perl
To build MySQL, we need to have three tools installed on our system. cmake, bison and perl. In my case, I had to install the cmake tool. The cmake tool has replaced the configure tool, because it is more portable.
$ cmake -L -- MySQL 5.5.9 -- Configuring done -- Generating done -- Build files have been written to: /home/vronskij/Downloads/mysql-5.5.9 -- Cache values CMAKE_BUILD_TYPE:STRING=RelWithDebInfo CMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql COMMUNITY_BUILD:BOOL=ON ENABLED_PROFILING:BOOL=ON ENABLE_DEBUG_SYNC:BOOL=ON INSTALL_LAYOUT:STRING=STANDALONE MYSQL_DATADIR:PATH=/usr/local/mysql/data MYSQL_MAINTAINER_MODE:BOOL=OFF WITH_ARCHIVE_STORAGE_ENGINE:BOOL=OFF WITH_BLACKHOLE_STORAGE_ENGINE:BOOL=OFF WITH_DEBUG:BOOL=OFF WITH_EMBEDDED_SERVER:BOOL=OFF WITH_EXTRA_CHARSETS:STRING=all WITH_FEDERATED_STORAGE_ENGINE:BOOL=OFF WITH_INNOBASE_STORAGE_ENGINE:BOOL=ON WITH_LIBEDIT:BOOL=ON WITH_LIBWRAP:BOOL=OFF WITH_PARTITION_STORAGE_ENGINE:BOOL=ON WITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON WITH_PIC:BOOL=OFF WITH_READLINE:BOOL=OFF WITH_SSL:STRING=no WITH_UNIT_TESTS:BOOL=ON WITH_VALGRIND:BOOL=OFF WITH_ZLIB:STRING=system
The -L option shows some of the default configure options. The system is going to be installed to /usr/loca/mysql directory. For us it is important to have InnoDB storage engine configured to be included.
$ cmake .
We configure the build. We leave all the default settings. In case we wanted to have also the MySQL embedded system, we would provide the -DWITH_EMBEDDED_SERVER=1 option.
$ make $ sudo make install
We make the system and install it.
$ sudo addgroup --system mysql $ sudo adduser --system mysql --no-create-home -ingroup mysql
We create a mysql system group and a mysql system user on our computer. Each process in Linux is owned by a specific user. The MySQL daemon will be owned by user mysql. Note that mysql is not a normal user account. It is a system user.
$ pwd /usr/local/mysql $ sudo chown -R mysql . $ sudo chgrp -R mysql .
We are located in the /usr/local/mysql directory. We change the group and owner of all files located in the mentioned directory. The -R option means recursive operation. This means that the two commands operate on all files and diretories and the contents of the directories.
$ sudo ./scripts/mysql_install_db --user=mysql
Here we install MySQL system tables. These tables are necessary for MySQL to be functional.
$ sudo chown -R root . $ sudo chown -R mysql data
We change the owner for all files back to the user root, except for the data directory. The MySQL server, which is owned by the mysql user, must have access to the data directory. The database files are stored in this directory.
Changing the root password
By default, the MySQL superuser root has an empty password. We need to set a password for the root account.
$ su Password: # /usr/local/mysql/bin/mysqld_safe & # exit exit $
We start the MySQL server. We change to the Linux root account and launch the mysqld_safe script. It is a MySQL server startup script.
$ sudo passwd root Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully $ su root Password: root@spartan:/usr/local/mysql#
On Ubuntu, the (Linux) root account is not enabled by default. Here we show, how to enable it. Then we can use the su (switch user) command to switch to root to start the MySQL daemon.
$ bin/mysqladmin -u root password '345jas7'
Using the mysqladmin we change the password for the root account. Note that command is launched without the sudo.
After we have installed the MySQL on our system and changed a password for the root account, there are still some modifications left to do.
MySQL has a configuration file called my.cnf. This is located in the /etc directory. By editing the options in this file, we can configure the server to our needs.
$ cp support-files/my-medium.cnf /etc/my.cnf $ cp support-files/my-medium.cnf ~/.my.cnf
In the support-files directory there are prepared configuration files. We can choose one of them, which best suits us. In the first command, we create MySQL global configuration file. In the second example, we create a personal file in the home directory of the user. This part is optional.
$ export PATH=$PATH:/usr/local/mysql/bin/ $ export MANPATH=$MANPATH:/usr/local/mysql/man/
Another useful thing to do is to add bin direcory to your PATH variable. This way we can launch MySQL commands and scripts without specifying the full path. In addition, we add the path to the manual pages of the MySQL tools and commands to the MANPATH variable. Now we can view MySQL man pages with the man command. Place both commands to your shell configuration file. In case of the bash it is .bashrc.
In this part of the MySQL tutorial, we have covered the installation of the MySQL database system.