MySQL en cluster

Contents

Pré-requis

L'installation a été faite sur des debian etch stables. Pour faire un cluster MySQL correct il est nécessaire de posséder un minimum de 3 serveurs. Le premier servira de manager et les deux autres de noeuds mysql ou de stockage.

Installation du manager

Installer les paquets mysql:

sudo aptitude install mysql-server

Empêcher mysql de se lancer au démarrage:

sudo update-rc.d -f mysql remove

Empêcher le serveur de noeud aussi:

sudo update-rc.d -f mysql-ndbd

Configurer le manager:

sudo cp /usr/share/doc/mysql-server-5.0/examples/ndb_mgmd.cnf /etc/mysql
vi /etc/mysql/ndb_mgmd.cnf
[NDBD DEFAULT]
NoOfReplicas=3                  # Nombre de noeuds NDB
DataMemory=10MB
IndexMemory=25MB
MaxNoOfTables=256
MaxNoOfOrderedIndexes=256
MaxNoOfUniqueHashIndexes=128

[MYSQLD DEFAULT]

[NDB_MGMD DEFAULT]

[TCP DEFAULT]

[NDB_MGMD]
Id=1                            # Le manager lui même
HostName=10.0.0.252             # Avec son adresse ip

[NDBD]
Id=2                            # Un premier noeud
HostName=10.0.0.224
DataDir= /var/lib/mysql-cluster

[NDBD]
Id=3                            # Le deuxième noeud
HostName=10.0.0.225
DataDir=/var/lib/mysql-cluster

[NDBD]
Id=4                            # Le troisième noeud
HostName=10.0.0.226
DataDir=/var/lib/mysql-cluster

[MYSQLD]
Id=5                            # Le premier noeud sql
HostName=10.0.0.224

[MYSQLD]
Id=6                            # Le deuxième noeud sql
HostName=10.0.0.225

[MYSQLD]
Id=7                            # Le troisième noeud sql
HostName=192.168.1.108

Installation d'un noeud

Comme pour le manager, installer les paquets mysql:

sudo aptitude install mysql-server

Configurer le noeud:

sudo vi /etc/mysql/my.cnf
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/english
skip-external-locking
ndbcluster
ndb-connectstring = 10.0.0.252

#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 10.0.0.224
#
# * Fine Tuning
#
key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 128K
thread_cache_size       = 8
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
#log            = /var/log/mysql/mysql.log
#
# Error logging goes to syslog. This is a Debian improvement :)
#
# Here you can see queries with especially long duration
#log_slow_queries       = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
#server-id              = 1
log_bin                 = /var/log/mysql/mysql-bin.log
# WARNING: Using expire_logs_days without bin_log crashes the server! See README.Debian!
expire_logs_days        = 10
max_binlog_size         = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
#
# * BerkeleyDB
#
# Using BerkeleyDB is now discouraged as its support will cease in 5.1.12.
skip-bdb
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
# You might want to disable InnoDB to shrink the mysqld process by circa 100MB.
#skip-innodb
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer              = 16M

#
# * NDB Cluster
#
# See /usr/share/doc/mysql-server-*/README.Debian for more information.
#
# The following configuration is read by the NDB Data Nodes (ndbd processes)
# not from the NDB Management Nodes (ndb_mgmd processes).
#
[MYSQL_CLUSTER]
ndb-connectstring = 10.0.0.252

# Read the manual for more InnoDB related options. There are many!
# You might want to disable InnoDB to shrink the mysqld process by circa 100MB.
#skip-innodb
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer              = 16M

#
# * NDB Cluster
#
# See /usr/share/doc/mysql-server-*/README.Debian for more information.
#
# The following configuration is read by the NDB Data Nodes (ndbd processes)
# not from the NDB Management Nodes (ndb_mgmd processes).
#
[MYSQL_CLUSTER]
ndb-connectstring = 10.0.0.252

#
# * IMPORTANT: Additional settings that can override those from this file!
#
!includedir /etc/mysql/conf.d/

Les sections qui nous intéressent sont mysqld et mysql_cluster:

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/english
skip-external-locking
ndbcluster
ndb-connectstring = 10.0.0.252

Notez les deux dernières lignes, ndb-connectstring correspondant à l'ip du manager. Et:

[MYSQL_CLUSTER]
ndb-connectstring = 10.0.0.252

Démarrage du cluster

Pour démarrer le cluster, commencer par lancer le manager:

sudo /etc/init.d/mysql-ndb-mgm start

Puis démarrez les noeuds, si c'est le premier démarrage de ce noeud:

sudo /etc/init.d/mysql-ndb start-initial

sinon:

sudo /etc/init.d/mysql-ndb start

Arrêt du cluster

Se connecter sur le manager et utiliser ndb_mgm:

sudo ndb_mgm
shutdown

ATTENTION: ne jamais couper le manager avant les noeuds, vous risquez de perdre des données !

Vérification du cluster

Pour vérifier l'état du cluster, se connecter sur le manager puis lancer ndb_mgm:

sudo ndb_mgm
show

et là vous devriez avoir une liste du type:

-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 10.0.0.252:1186
Cluster Configuration
---------------------
[ndbd(NDB)]	1 node(s)
id=2	@10.0.0.224  (Version: 5.0.32, Nodegroup: 0, Master)

[ndb_mgmd(MGM)]	1 node(s)
id=1	@10.0.0.252  (Version: 5.0.32)

[mysqld(API)]	1 node(s)
id=5	@10.0.0.224  (Version: 5.0.32)

Si un noeud n'est pas connecté, celui-ci est signalé dans la liste:

Cluster Configuration
---------------------
[ndbd(NDB)]	1 node(s)
id=2 (not connected, accepting connect from 10.0.0.224)

[ndb_mgmd(MGM)]	1 node(s)
id=1	@10.0.0.252  (Version: 5.0.32)

[mysqld(API)]	1 node(s)
id=5 (not connected, accepting connect from 10.0.0.224)

Tests avec une base

Se connecter sur un des noeuds, jamais sur le manager:

mysql -h 10.0.0.252 -u root -p
create database tests
create table toto (id int) ENGINE=NDBCLUSTER

Le cluster fonctionne en spécifiant le moteur utilisé pour une table. Pour modifier une table existante:

alter table toto ENGINE=NDBCLUSTER

Forcer l'utilisation d'une table en cluster

Pour éviter de faire des alter table à gogo, il est possible de spécifier à mysql que le moteur par défaut est le cluster.

sudo vi /etc/mysql/my.cnf

Rajouter dans la section mysqld:

 [mysqld]
 
default-table-type=NDBCLUSTER

Erreurs courantes

  • Si vous voyez une erreur 2350 sur le manager lors de la tentative de connection d'un noeud, c'est que celui-ci n'est pas initialisé correctement:
sudo /etc/init.d/mysql-ndb start-initial

sur le noeud posant problème devrait corriger la situation.

  • Les clusters mysql ne sont pas sécurisés
  • Les clusters mysql ont besoin de beaucoup de bande passante, une carte 100mb est le minimum pour avoir des performances correctes

Liens

Un Howto un peu vieux mais bien écrit (en anglais):

La doc officielle:

Un autre Howto un peu vieux aussi (en français):

MysqlCluster et la mémoire: