Menú principal

Instalación de MySQL en Slackware Linux

26 May
Published by antonio.hernandez in

A diferencia de PostgreSQL, MySQL forma parte oficial de Slackware Linux por lo que en una instalación completa del mismo ya estará disponible en el sistema. En caso de no ser así puede ser instalado desde los CDs/DVD de Slackware Linux (se encuentra en el directorio slackware/ap) o bien descargando el paquete desde internet. De cualquier forma daré por hecho que ya se encuentra instalado he indicaré los pasos para su ejecución.

Primero comprobamos que ya se encuentre instalado el paquete mysql:

root@localhost:~# ls -l /var/log/packages/mysql*  
-rw-r--r-- 1 root root 13081 Jul 22 17:47 /var/log/packages/mysql-5.1.56-i486-1

Comprobamos la existencia del usuario y grupo mysql:

root@localhost:~# grep mysql /etc/passwd /etc/group
/etc/passwd:mysql:x:27:27:MySQL:/var/lib/mysql:/bin/false
/etc/group:mysql:x:27:

Comprobamos la estructura de directorios donde residirán nuestras bases de datos así
como el socket (unix) y archivo pid del demonio de MySQL(mysqld):

root@localhost:~# find /var/run/mysql /var/lib/mysql -ls  
 38882    0 drwxr-xr-x   2 mysql    mysql          48 Apr  5 17:33 /var/run/mysql  
 38881    0 drwxr-x---   2 mysql    mysql          48 Apr  5 17:33 /var/lib/mysql

Asignamos permisos de ejecución al script de inicio de MySQL:

root@localhost:~# chmod 755 /etc/rc.d/rc.mysqld

Utilizamos uno de los archivos de configuración de MySQL como base:

root@localhost:~# ls -l /etc/my-*  
-rw-r--r-- 1 root root 4688 Apr  5 17:33 /etc/my-huge.cnf  
-rw-r--r-- 1 root root 4662 Apr  5 17:33 /etc/my-large.cnf  
-rw-r--r-- 1 root root 4673 Apr  5 17:33 /etc/my-medium.cnf  
-rw-r--r-- 1 root root 2441 Apr  5 17:33 /etc/my-small.cnf  
root@localhost:~# cp /etc/my-small.cnf /etc/my.cnf  
root@localhost:~# chown mysql:mysql /etc/my.cnf  
root@localhost:~# chmod 440 /etc/my.cnf

Inicializamos la base de datos propia de MySQL:

root@localhost:~# mysql_install_db --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost password 'new-password'
 
Alternatively you can run:
/usr/bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
 
Please report any problems with the /usr/bin/mysqlbug script!

Iniciamos al demonio de MySQL:

root@localhost:~# /etc/rc.d/rc.mysqld start
110816 00:53:21 mysqld_safe Logging to '/var/lib/mysql/localhost.err'.
110816 00:53:21 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

Corroboramos su ejecución:

root@localhost:~# ps -eo pid,uid,user,comm | grep mysql 
26401     0 root     mysqld_safe
26574    27 mysql    mysqld

El paquete de MySQL cuenta con un shell script (mysql_secure_installation) para mejorar la
seguridad en la instalación de MySQL, lo que nos permitirá asignar una contraseña a la cuenta
root (no confundir con el super usuario), borrar cuentas anónimas,
bloquear conexiones externas por parte del usuario root así como borrar una base de
datos test instalada por default en MySQL.

root@localhost:~# mysql_secure_installation
 
 
 
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
 
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
 
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] Y
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] Y
 ... Success!
 
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] Y
 ... Success!
 
Cleaning up...
 
 
 
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
 
Thanks for using MySQL!

Detenemos e iniciamos el demonio de MySQL solo para comprobar que el proceso de reinicio funcione:

root@localhost:~# /etc/rc.d/rc.mysqld stop
110816 01:09:28 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended
root@localhost:~# /etc/rc.d/rc.mysqld start
110816 01:09:33 mysqld_safe Logging to '/var/lib/mysql/localhost.err'.
110816 01:09:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

Finalmente comprobamos que podemos acceder a MySQL:

usuario@localhost:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.56 Source distribution
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> quit
Bye

Existen dos formas en que el demonio de MySQL acepte conexiones, usando un
socket de unix (/var/run/mysql/mysql.sock) o un socket TCP (puerto 3306):

root@localhost:~# ls -l /var/run/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Aug 16 01:09 /var/run/mysql/mysql.sock=
root@localhost:~# netstat -plutn | grep mysqld

Por default MySQL en Slackware Linux solo tiene activado el socket unix, más no el
socket TCP.

Para activar el socket TCP de MySQL en Slackware Linux editamos el archivo
/etc/rc.d/rc.mysqld y al inicio de la línea SKIP="--skip-networking" colocamos un
símbolo de # y reiniciamos el demonio de MySQL:

root@localhost:~# /etc/rc.d/rc.mysqld restart
110816 01:31:41 mysqld_safe Logging to '/var/lib/mysql/localhost.err'.
110816 01:31:41 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
root@localhost:~# netstat -plutn | grep mysqld
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      31116/mysqld

Ahora tenemos los dos sockets de conexión a MySQL activos. Nótese que el socket TCP esta
en escucha de conexiones desde el exterior en todas las interfaces de red (0 0.0.0.0:3306).

Si se desea tener al socket TCP escuchando conexiones solo de forma local (localhost o 127.0.0.0/8)
se debe editar el archivo de configuración de MySQL y agregar la línea bind-address = 127.0.0.1

Extracto de un archivo de configuración de MySQL:

# my.cnf
[client]
port = 3306
socket = /var/run/mysql/mysql.sock
 
[mysqld]
bind-address = 127.0.0.1
port = 3306
socket = /var/run/mysql/mysql.sock
...

Reiniciamos el servicio y comprobamos el resultado:

root@localhost:~# /etc/rc.d/rc.mysqld restart
110816 01:39:32 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended
110816 01:39:32 mysqld_safe Logging to '/var/lib/mysql/localhost.err'.
110816 01:39:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
root@localhost:~# netstat -plutn | grep mysqld
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      6480/mysqld