9
Instalar MySQL CentOS | Moisés Araya [1] Instalar MySQL 5.1 en CentOS 6.4x64 A continuación se presenta una guía básica donde se muestra la instalación, configuración y algunas tareas básicas de uso de MySQL en un servidor Linux. Procedimiento. Instalar librerías. [root@centos ~]# yum -y install mysql-server Installed: mysql-server.x86_64 0:5.1.73-3.el6_5 Dependency Installed: make.x86_64 1:3.81-20.el6 mysql.x86_64 0:5.1.73-3.el6_5 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6 Dependency Updated: mysql-libs.x86_64 0:5.1.73-3.el6_5 openssl.x86_64 0:1.0.1e-30.el6_6.4 Complete! [root@centos ~]# Editar archivo de configuración y agregar set de caracteres utf8 (útil para caracteres acentuados). [root@centos ~]# vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 character-set-server=utf8 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid Iniciar MySQL. [root@centos ~]# /etc/rc.d/init.d/mysqld start Iniciando base de datos MySQL: WARNING: The host 'centos.lab' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK

Instalar MySQL CentOS

Embed Size (px)

Citation preview

Page 1: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[1]

Instalar MySQL 5.1 en CentOS 6.4x64

A continuación se presenta una guía básica donde se muestra la instalación, configuración y algunas tareas básicas de uso de MySQL en un servidor Linux.

Procedimiento.

Instalar librerías.

[root@centos ~]# yum -y install mysql-server Installed: mysql-server.x86_64 0:5.1.73-3.el6_5 Dependency Installed: make.x86_64 1:3.81-20.el6 mysql.x86_64 0:5.1.73-3.el6_5 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6 Dependency Updated: mysql-libs.x86_64 0:5.1.73-3.el6_5 openssl.x86_64 0:1.0.1e-30.el6_6.4 Complete! [root@centos ~]#

Editar archivo de configuración y agregar set de caracteres utf8 (útil para caracteres acentuados).

[root@centos ~]# vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 character-set-server=utf8 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid

Iniciar MySQL.

[root@centos ~]# /etc/rc.d/init.d/mysqld start Iniciando base de datos MySQL: WARNING: The host 'centos.lab' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK

Page 2: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[2]

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 centos.lab 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! [ OK ] Iniciando mysqld: [ OK ]

Configurar inicio de servicio.

[root@centos ~]# chkconfig mysqld on

Configuración segura de MySQL.

Seteo de contraseña de usuario root.

Eliminar usuarios anónimos.

Deshabilitar el acceso remoto de root

Eliminar las bases de datos de test

Page 3: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[3]

[root@centos ~]# 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!

Page 4: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[4]

Conectarse a MySQL.

[root@centos ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

Mostrar usuarios/DB y salir.

mysql> select user,host,password from mysql.user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | +------+-----------+-------------------------------------------+ 2 rows in set (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | +--------------------+ 2 rows in set (0.00 sec) mysql> exit Bye [root@centos ~]#

Revisar version

mysql> select version(), current_date; +-----------+--------------+ | version() | current_date | +-----------+--------------+ | 5.1.73 | 2014-11-24 | +-----------+--------------+ 1 row in set (0.03 sec)

Page 5: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[5]

Crear DB y mostrar DB existentes.

mysql> create database usuarios; Query OK, 1 row affected (0.00 sec) mysql> use usuarios Database changed

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | usuarios | +--------------------+ 3 rows in set (0.00 sec)

Seleccionar Base de datos.

mysql> use mysql; Database changed

Crear tablas/Mostrar tablas.

mysql> show tables; Empty set (0.00 sec) mysql> CREATE TABLE administracion (nombre VARCHAR(20), apellido VARCHAR(20), cargo VARCHAR(20), ingreso DATE); Query OK, 0 rows affected (0.08 sec) mysql> show tables; +--------------------+ | Tables_in_usuarios | +--------------------+ | administracion | +--------------------+ 1 row in set (0.00 sec)

Mostrar la estructura de la tabla creada,

mysql> describe administracion; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | nombre | varchar(20) | YES | | NULL | | | apellido | varchar(20) | YES | | NULL | | | cargo | varchar(20) | YES | | NULL | | | ingreso | date | YES | | NULL | | +----------+-------------+------+-----+---------+-------+

Page 6: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[6]

Cargar datos en la DB.

mysql> INSERT INTO administracion VALUES ('David','Errazuriz','Ingeniero','2014-01-01'); Query OK, 1 row affected (0.00 sec)

Obtener información de la tabla modificada.

mysql> SELECT * FROM administracion; +--------+-----------+-----------+------------+ | nombre | apellido | cargo | ingreso | +--------+-----------+-----------+------------+ | David | Errazuriz | Ingeniero | 2014-01-01 | +--------+-----------+-----------+------------+ 1 row in set (0.00 sec)

Borrar datos de la tabla.

mysql> DELETE FROM administracion; Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM administracion; Empty set (0.00 sec)

Actualizar algún registro.

mysql> UPDATE administracion SET cargo="gerente" -> WHERE nombre="David"; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM administracion; +--------+-----------+---------+------------+ | nombre | apellido | cargo | ingreso | +--------+-----------+---------+------------+ | David | Errazuriz | gerente | 2014-01-01 | +--------+-----------+---------+------------+ 1 row in set (0.00 sec)

Buscar información en una tabla. Para esto se han añadido tres registros a la tabla.

mysql> SELECT * FROM administracion; +---------+-----------+-----------+------------+ | nombre | apellido | cargo | ingreso | +---------+-----------+-----------+------------+ | David | Errazuriz | Ingeniero | 2014-01-01 | | Arturo | Gonzalez | Gerente | 2014-03-09 | | Gonzalo | Perez | Soporte | 2013-03-09 | +---------+-----------+-----------+------------+

Page 7: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[7]

mysql> SELECT * FROM administracion WHERE nombre='Arturo'; +--------+----------+---------+------------+ | nombre | apellido | cargo | ingreso | +--------+----------+---------+------------+ | Arturo | Gonzalez | Gerente | 2014-03-09 | +--------+----------+---------+------------+ 1 row in set (0.00 sec)

Búsqueda compuesta

mysql> SELECT * FROM administracion WHERE cargo='Ingeniero' AND nombre='David'; +--------+-----------+-----------+------------+ | nombre | apellido | cargo | ingreso | +--------+-----------+-----------+------------+ | David | Errazuriz | Ingeniero | 2014-01-01 | +--------+-----------+-----------+------------+ 1 row in set (0.00 sec)

Seleccionar solo algunos campos

mysql> SELECT nombre, cargo FROM administracion; +---------+-----------+ | nombre | cargo | +---------+-----------+ | David | Ingeniero | | Arturo | Gerente | | Gonzalo | Soporte | +---------+-----------+ 3 rows in set (0.00 sec)

Ordenar resultados

mysql> SELECT nombre FROM administracion ORDER BY ingreso; +---------+ | nombre | +---------+ | Gonzalo | | David | | Arturo | +---------+

Respaldar y restaurar

Respaldar.

[root@centos ~]# mysqldump -u root -proot usuarios > dump_dbusuarios.sql

Ver archivo creado.

[root@centos ~]# cat dump_dbusuarios.sql -- MySQL dump 10.13 Distrib 5.1.73, for redhat-linux-gnu (x86_64) --

Page 8: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[8]

-- Host: localhost Database: usuarios -- ------------------------------------------------------ -- Server version 5.1.73 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `administracion` -- DROP TABLE IF EXISTS `administracion`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `administracion` ( `nombre` varchar(20) DEFAULT NULL, `apellido` varchar(20) DEFAULT NULL, `cargo` varchar(20) DEFAULT NULL, `ingreso` date DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `administracion` -- LOCK TABLES `administracion` WRITE; /*!40000 ALTER TABLE `administracion` DISABLE KEYS */; INSERT INTO `administracion` VALUES ('David','Errazuriz','Ingeniero','2014-01-01'),('Arturo','Gonzalez','Gerente','2014-03-09'),('Gonzalo','Perez','Soporte','2013-03-09'); /*!40000 ALTER TABLE `administracion` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2014-12-10 8:56:03

Restaurar BD

Page 9: Instalar MySQL CentOS

Instalar MySQL CentOS | Moisés Araya

[9]

Eliminar, crear y luego restaurar.

mysql> drop database usuarios; Query OK, 1 row affected (0.08 sec)

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | +--------------------+ 2 rows in set (0.00 sec)

mysql> create database usuarios; Query OK, 1 row affected (0.02 sec)

[root@centos ~]# mysql -u root -proot usuarios < dump_dbusuarios.sql

mysql> select * from administracion; +---------+-----------+-----------+------------+ | nombre | apellido | cargo | ingreso | +---------+-----------+-----------+------------+ | David | Errazuriz | Ingeniero | 2014-01-01 | | Arturo | Gonzalez | Gerente | 2014-03-09 | | Gonzalo | Perez | Soporte | 2013-03-09 | +---------+-----------+-----------+------------+ 3 rows in set (0.00 sec)