Installing the MySQL software

Installing the MySQL packages

The dev-db/mysql package, like all other packages in a Gentoo Linux environment, will automatically install any required dependencies. One of these dependencies is a correctly installed and configured logging daemon. If you have not already installed one now is a good time to follow the System logging with syslog-ng guide before proceeding with the MySQL installation.

Caution:
If you intend to implement the Logging to a database section of the System logging with syslog-ng guide then you will, fairly obviously, need to return back to this guide and correctly install MySQL before continuing with that section. You should also make sure that you read and understand the sections on log priorities for both the app-admin/syslog-ng and dev-db/mysql packages so that log-message loops can be avoided.
 

Before we install any packages we should ensure that the correct use-flags will be used so that all required functionality is made available and unnecessary functionality is not included. The dev-db/mysql package and its dependencies provide a variety of use-flags only some of which will be discussed further here. As usual feel free to add and remove use-flags at will although the minimum set which are required for using this guide in its entirety are shown below.

lisa emerge -pv mysql
 
These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild      ] dev-db/mysql-init-scripts-1.2
[ebuild      ] dev-perl/Net-Daemon-0.43
[ebuild      ] perl-core/Storable-2.20
[ebuild      ] perl-core/File-Spec-3.31
[ebuild      ] virtual/perl-Sys-Syslog-0.270.0-r1
[ebuild      ] virtual/yacc-0
[ebuild      ] app-arch/libarchive-2.8.4-r1  USE="acl bzip2 lzma zlib -expat -static -static-libs -xattr"
[ebuild      ] virtual/perl-Storable-2.20
[ebuild      ] virtual/perl-File-Spec-3.31
[ebuild      ] dev-util/cmake-2.8.4-r1  USE="ncurses -emacs -qt4 -test -vim-syntax"
[ebuild      ] dev-perl/PlRPC-0.202.0
[ebuild      ] dev-perl/DBI-1.615  USE="-test"
[ebuild      ] dev-db/mysql-5.1.56  USE="community perl ssl -big-tables -cluster -debug -embedded -extraengine -latin1 -max-idx-128 -minimal -pbxt -profiling -static -test -xtradb"
[ebuild      ] virtual/mysql-5.1  USE="-embedded -minimal -static"
[ebuild      ] dev-perl/DBD-mysql-4.01.7

Once you are confident that the correct use-flags are set for the dev-db/mysql package, and any dependencies it may require, you can proceed with the installation by issuing the emerge command shown below.

lisa emerge mysql

Creating a basic MySQL configuration

The MySQL ebuild creates two default daemon configuration files at /etc/conf.d/mysql and /etc/mysql/my.cnf which define some variables which may be of interest to us before we issue the command to run the configuration part of the installation process. These configuration variables are briefly described below along with some more sensible defaults where appropriate.

/etc/conf.d/mysql

The /etc/conf.d/mysql file is used to provide configuration information to the mysql initialisation script. The default parameters will start a single mysqld process using the default options. For the purposes of this guide the default options will be sufficient.

/etc/mysql/my.cnf

datadir
The datadir variable is used to specify the path to the database storage location. By default the databases will be located under /var/lib/mysql/ which, on anything other than a hobbyist system at least, is less than ideal. We recommend creating a separate partition or logical volume to contain the databases to both protect them from other files filling the filing system, which can result in database corruption, and ensure that the disk space they will be occupying will not be fragmented by existing files. This approach also enables backups to be made more easily, especially when using LVM or EVMS, and can also help improve overall system security and stability. Unless this partition or volume will be mounted at /var/lib/mysql this value will need to be changed.
bind-address
The bind-address variable specifies the network address on which the mysql daemon should listen for client connections. The default is the loopback address which will restrict client connections to the local machine only. To bind to all available network addresses the special address 0.0.0.0 may be used.

Once you are happy that the configuration is to your liking you can install a default set of internal databases for the MySQL server by issuing the following command.

lisa mkdir -p /mnt/databases/mysql/5
lisa chmod a+X /mnt
lisa chmod a+X /mnt/databases
lisa chmod a+X -R /mnt/databases/mysql
lisa emerge --config mysql
Caution:
It is very important that the directories which contain the mysql database files can be traversed by the mysql user. The chmod a+X commands above ensure this. If the mysql user is not able to traverse the directories above the database files strange permission errors (such as Fatal error: Can't open and lock privilege tables: Table 'host' is read only or Can't create/write to file '/mnt/databases/mysql/5/mysql/db.MYI') will occur.
 

Configuring basic security settings

While the ebuild creates a default configuration for you, including the mysql user and group, it does not create a user account for administrative purposes. So that we can perform local administrative duties using an account other than root or the mysql account which the mysqld daemon process will use we need to create such an account as shown below.

lisa useradd -d /home/msqladmin -N -g users -G mysql -m mysqladmin
lisa passwd mysqladmin
New UNIX password: ******* 
Retype new UNIX password: ******* 
passwd: password updated successfully 

Configuring basic network settings

Now that some basic security options have been set all that remains for our basic configuration is to modify the bind-address setting in the my.cnf file so that remote IP clients can connect to the server. Without this modification the server will only bind to the local loopback address and therefore only local clients would be able to connect.

/etc/mysql/my.cnf
[mysqld]

bind-address = 127.0.0.1
bind-address = 0.0.0.0

Configuring syslog settings

The default configuration created during the MySQL installation process instructs the mysqld daemon to send any log output to a file located at /var/log/mysql/mysql.err. This may be acceptable in a development environment but in a production environment we almost certainly want the logs sent to a system logging daemon sych as syslog-ng. The following example shows the modifications required to instruct the mysqld daemon to send any log output to the syslog daemon.

/etc/mysql/my.cnf
[mysqld_safe]
#err-log = /var/log/mysql/mysql.err
syslog
Caution:
If you intend to implement the Logging to a database section of the System logging with syslog-ng guide then you should make sure that you read and understand the sections on log priorities for both the syslog-ng and mysql packages so that log-message loops can be avoided.
 

Starting the MySQL daemon

Now that the default configuration file along with a set of internal databases has been created and the basic security, networking and logging options have been configured you can start the MySQL daemon and add it to the default run-level by issuing the following commands.

lisa /etc/init.d/mysql start
lisa rc-update add mysql default