Saltar al contenido

Mysql

Clustering Mysql con Vitess

  • admin 

Clustering Mysql con Vitess

Viendo algunos de los proyectos con Golang que utilizan en Google, este me ha llamado la atención, porque es lo que utilizan desde 2011 con Youtube para mantener la BBDD de Mysql en cluster:

http://vitess.io/

https://github.com/youtube/vitess/

Entre sus funcionalidades, no solo mantiene el tema de la replicación y el shardening entre los nodos (en Youtube son decenas de miles de nodos con MySQL), sino que optimiza las operaciones que pueden sobrecargar la BBDD así como el uso de la caché, la gestión de los backups, etc…

Conceptos básicos sobre MySQL Cluster

¿Por qué MySQL Cluster y no una base de datos normal y corriente? Podemos decir que nuestro proyecto mantiene unas necesidades que no se cubren con un sistema de bases de datos corriente. Nuestro principal problema reside en la respuesta a un gran nombre de consultas al sistema que estamos desarrollando; para que ésto sea eficiente necesitamos una tecnología que nos permita la distribución de estas peticiones para tener un tiempo de respuesta menor. Por esta razón pensamos en MySQL Cluster como una buena y fácil solución.

Características generales de MySQL Cluster

MySQL Cluster está formado por un motor NDB, que es un motor de almacenamiento en memoria caracterizado por la persistencia de los datos. Cada parte del cluster se denomina nodo y podemos encontrar de diferentes tipos:

– El nodo administrador tiene la función principal de administrar otros nodos dentro del cluster. Además, ofrece datos sobre la configuración de cada uno, inicializa o detiene su funcionamiento o permite ejecutar copias de seguridad. Es muy importante, por contener la configuración de los otros nodos, que sea el primero en ser arrancado.

– El nodo de datos es el nodo que almacena los datos del cluster. Tendremos tantos nodos de este estilo como réplicas de la información que necesitemos para el sistema.

– El nodo SQL es el nodo que accede a los datos del cluster. En un MySQL Cluster, el nodo cliente es un servidor MySQL tradicional con motor NDB Cluster.

Con esta estructura obtenemos que la configuración del sistema de cluster reside en la configuración de sus nodos y en la comunicación que establecemos entre ellos. Debemos tener en cuenta que se trata de un proceso centralizado en el nodo de administración por lo que es necesario tenerlo arrancado antes de empezar la creación de los nodos de datos.

 

Esquema básico cluster

How do I import delimited data into MySQL?

  • admin 

If you have data that you need to bring into your MySQL database, there are a few ways to do it. Exporting data out of mysql is another topic, described here.

1. Using the LOAD DATA INFILE SQL statement

For security reasons, no one has the mysql FILE priv, which means you cannot «LOAD DATA INFILE». You can, however, use a «LOAD DATA LOCAL INFILE» statement as long as you have amysql prompt on our system and have uploaded the data file to your account here first.

The «LOAD DATA LOCAL INFILE» statement will only work from a MySQL prompt on our local system. It will not work from any web-based tool such as phpMyAdmin, and will never pull a file in directly off your own computer.

To import a file this way, first upload your data file to your home directory on our system with FTP or SCP. Then get a shell prompt on our system, and then a MySQL Monitor prompt so that you can issue the SQL that will import your file.

For example, suppose you have a data file named importfile.csv that contains 3 comma separated columns of data on each line. You want to import this textfile into your MySQL table named test_table, which has 3 columns that are named field1field2 and field3.

To import the datafile, first upload it to your home directory, so that the file is now located at /importfile.csv on our local system. Then you type the following SQL at the mysql prompt:

LOAD DATA LOCAL INFILE ‘/importfile.csv
INTO TABLE test_table
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘\n’
(field1, filed2, field3);

The above SQL statement tells the MySQL server to find your INFILE on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, and to put it into your MySQL test_table as columns field1, field2, and field3 respectively. Many of the above SQL clauses are optional and you should read the MySQL documentation on the proper use of this statement.

2. Using a script to parse and import the file

You can also write a script in any programming language that can connect to MySQL (such as PHP) to open your data file, break it up into an array of lines that each represent a row of data, split each line up by the delimiter character (such as a comma ‘,’, tab ‘\t’, semicolon ‘;’, space ‘ ‘, etc.), and then perform invididual MySQL INSERT queries (one INSERT for each line) to insert all your data from the file into the appropriate table fields.

Such scripts are not difficult to write in less than 15 lines and can import data from text files just as effectively as a LOAD DATA LOCAL INFILE command. A working example script written in PHP appears below in the Annotations.

3. Importing a mysqldump

If your data file actually comes from another MySQL database, and not from Excel or any other source, then the most direct way to export and import your data would be to dump out your table or entire MySQLdatabase on the original database server using the mysqldump command, FTP the resulting dump file to your account here, and then import the dump file at a shell prompt.

For instructions on creating the dumpfile using the mysqldump command, see this FAQ. For instructions on how to import a dump made with mysqldump, see this FAQ.