Et cluster er en klump af maskiner der samarbejder om en bestemt opgave, dvs. vores CDN var faktisk en slags cluster, nu vil jeg tage den skridtet videre og lave vores CDN om til et reelt cluster der kører en WordPress installation.
I forhold til CDN’et nuværende opsætning mangler vi support for PHP og en Mysql database før vi kan køre WordPress på clusteret. Jeg vil starte med at sætte mysql op på begge maskiner, og bagefter sætte master-slave replikation op.
Derefter vil jeg bruge mysql-proxy til at opdele læse/skrive queries og slutteligt vil jeg installere php og WordPress. Jeg vælger at lab1 skal være min master og lab2 skal være min slave.
Først mysql: (udføres på begge maskiner)
root@lab1:~# apt-get install mysql-server root@lab2:~# apt-get install mysql-server
Du bliver bedt tildele et root-password til MySQL databaserne, hvorefter resten af installationen skulle køre af sig selv. Herefter opsætter jeg lab1 og lab2 ved at redigere /etc/mysql/my.cnf:
# Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 127.0.0.1 #
rettes til:
# Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 0.0.0.0 #
Hermed tillader vi MySQL at kommunikere via nettet, dvs indgående forbindelser til mysql-serveren er nu mulige, og på lab1 (kun lab1 … lab2 fortsætter vi med om lidt) fortsætter vi lidt længere nede i filen:
#server-id = 1 #log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name
rettes til:
server-id = 1 log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M binlog_do_db = wordpress #binlog_ignore_db = include_database_name
Hver node i clusteret skal have et unikt server id, masteren hedder 1 (det er dog ikke et krav … masteren kan have et hvilket som helst nummer) og vi vælger at databasen “wordpress” skal replikeres.
Genstart nu mysql
root@lab1:~# /etc/init.d/mysql restart Stopping MySQL database server: mysqld. Starting MySQL database server: mysqld.
Derefter oprettet vi vores database og tilføjer replikation-privilegier:
root@lab1:~# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 35 Server version: 5.1.61-0+squeeze1 (Debian) Copyright (c) 2000, 2011, 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> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slavepass'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
Intet af det ovenstående burde være svært på nuværende tidspunkt … vi opretter databasen, tillader replikation og flush’er privilegier. Du bør (selvfølgelig) vælge fornuftige password og sætte et hostname på din grant hvis du laver et offentligt produktionsmiljø.
Nu tager vi fat i lab2 og laver den modsatte øvelse, red følgende i /etc/mysql/my.cnf:
# The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. #server-id = 1 #log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name
Til:
server-id = 2 #log_bin = /var/log/mysql/mysql-bin.log #expire_logs_days = 10 #max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name master_host=109.202.159.61 master_user=slave master_password=slavepass master_connect_retry=60 replicate_do_db=wordpress
Og opret databasen:
root@lab2:~# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 40 Server version: 5.1.61-0+squeeze1 (Debian) Copyright (c) 2000, 2011, 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> create database wordpress; Query OK, 1 row affected (0.00 sec)
Genstart MySQL:
root@lab2:~# /etc/init.d/mysql restart Stopping MySQL database server: mysqld. Starting MySQL database server: mysqld.
Til sidst logger vi ind på mysql-consolen på lab2 og sætter det hele igang:
root@lab2:~# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 37 Server version: 5.1.61-0+squeeze1 (Debian) Copyright (c) 2000, 2011, 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> CHANGE MASTER TO MASTER_HOST='lab1.mikjaer.com', MASTER_PORT=3307, MASTER_USER='slave', MASTER_PASSWORD='slavepass', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0; Query OK, 0 rows affected (0.56 sec) mysql> START SLAVE; Query OK, 0 rows affected (0.00 sec)
Hvis du vil tjekke at det virker kan du evt. prøve og fyre hhv. “show master status” og “show slave status” af, imellemtiden vil jeg teste at det virker:
Først “ingenting her”:
root@lab2:~# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 40 Server version: 5.1.61-0+squeeze1 (Debian) Copyright (c) 2000, 2011, 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> use wordpress; Database changed mysql> show tables; Empty set (0.00 sec)
Ingen ting der:
root@lab1:~# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 37 Server version: 5.1.61-0+squeeze1-log (Debian) Copyright (c) 2000, 2011, 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> use wordpress; Database changed mysql> show tables; Empty set (0.00 sec)
Så opretter vi en sød lille tabel på lab1:
mysql> create table test (foo INT,bar INT); Query OK, 0 rows affected (0.11 sec) mysql> show tables; +---------------------+ | Tables_in_wordpress | +---------------------+ | test | +---------------------+ 1 row in set (0.00 sec)
Og nu på lab2, viola:
root@lab2:~# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 44 Server version: 5.1.61-0+squeeze1 (Debian) Copyright (c) 2000, 2011, 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> use wordpress; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------------+ | Tables_in_wordpress | +---------------------+ | test | +---------------------+ 1 row in set (0.00 sec)
Tada 😀 – Det virker, hvis ikke det gør er her et par nyttige queries til fejlfinding:
show slave status \G show master status \G SHOW VARIABLES LIKE 'server_id';
Næste skridt på listen er MySQL Proxy, opsætningen af denne foregår på samme måde på begge maskiner:
root@labX:~# apt-get install mysql-proxy