2.创建mysql用户和mysql数据共享目录
node1节点:
[root@node1 ~]# groupadd -g 502 mysql
[root@node1 ~]# useradd -r -s /sbin/nologin -u 502 -g mysql mysql
[root@node1 ~]# mkdir /mysql/data -p
node2节点:
[root@node2 ~]# groupadd -g 502 mysql
[root@node2 ~]# useradd -r -s /sbin/nologin -u 502 -g mysql mysql
[root@node2 ~]# mkdir /mysql/data -p
3.安装mysql数据库软件
node1节点:
[root@node1 ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.4-m14-linux-glibc2.5-i686.tar.gz
[root@node1 ~]# tar xvf mysql-5.7.4-m14-linux-glibc2.5-i686.tar.gz
[root@node1 ~]# mv mysql-5.7.4-m14-linux-glibc2.5-i686 /usr/local/mysql-5.7.4
[root@node1 ~]# cd /usr/local/mysql-5.7.4
[root@node1 mysql-5.7.4]# cp -a support-files/my-default.cnf /etc/my.cnf
[root@node1 mysql-5.7.4]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql-5.7.4/ --datadir=/mysql/data/
[root@node1 mysql-5.7.4]# ll /mysql/data/
total 122904
-rw-rw----. 1 mysql mysql 56 Dec 29 00:06 auto.cnf
-rw-rw----. 1 mysql mysql 12582912 Dec 29 00:06 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Dec 29 00:06 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Dec 29 00:03 ib_logfile1
-rw-rw----. 1 mysql mysql 12582912 Dec 29 00:06 ibtmp1
drwx------. 2 mysql mysql 4096 Dec 29 00:04 mysql
-rw-rw----. 1 root root 6 Dec 29 00:06 mysqld_safe.pid
-rw-r-----. 1 mysql root 3919 Dec 29 00:06 node1.err
-rw-rw----. 1 mysql mysql 6 Dec 29 00:06 node1.pid
drwx------. 2 mysql mysql 4096 Dec 29 00:04 performance_schema
[root@node1 mysql-5.7.4]# vim /etc/my.cnf
basedir = /usr/local/mysql-5.7.4
datadir = /var/lib/mysql
port = 3306
[root@node1 mysql-5.7.4]# chown mysql:mysql /var/lib/mysql/
[root@node1 mysql-5.7.4]# cp -a support-files/mysql.server /etc/init.d/mysqld
[root@node1 mysql-5.7.4]# chkconfig --add mysqld
[root@node1 mysql-5.7.4]# chkconfig mysqld off
[root@node1 mysql-5.7.4]# vim /etc/exports
/mysql/data 192.168.1.0/24(no_root_squash,rw)
[root@node1 mysql-5.7.4]# exportfs -arv
exporting 192.168.1.0/24:/mysql/data
[root@node1 mysql-5.7.4]# mount.nfs 192.168.1.105:/mysql/data/ /var/lib/mysql/
[root@node1 mysql-5.7.4]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL..... SUCCESS!
[root@node1 mysql-5.7.4]# netstat -antp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 27729/mysqld
[root@node1 mysql-5.7.4]# /usr/local/mysql-5.7.4/bin/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.7.4-m14 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> exit
Bye
[root@node1 mysql-5.7.4]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@node1 mysql-5.7.4]# umount /var/lib/mysql/
[root@node1 mysql-5.7.4]# scp /etc/my.cnf node2:/etc/
root@node2's password:
my.cnf 100% 1142 1.1KB/s 00:00
[root@node1 mysql-5.7.4]# scp /etc/init.d/mysqld node2:/etc/init.d/mysqld
root@node2's password:
mysqld 100% 11KB 10.7KB/s 00:00
[root@node1 mysql-5.7.4]#
node2节点:
[root@node2 ha.d]# showmount -e node1
Export list for node1:
/mysql/data 192.168.1.0/24(no_root_squash,rw)
[root@node2 ha.d]# mount.nfs 192.168.1.105:/mysql/data/ /var/lib/mysql/
[root@node2 ha.d]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL........ SUCCESS!
[root@node2 ha.d]# netstat -antup | grep 3306
tcp 0 0 :::3306 :::* LISTEN 7355/mysqld
[root@node2 ha.d]#
4.安装和配置HeartBeat集群软件
node1节点:
[root@node1 ~]# yum install heartbeat*
[root@node1 ~]# cp -a /usr/share/doc/heartbeat-3.0.4/{authkeys,haresources,ha.cf} /etc/ha.d/
[root@node1 ~]# cd /etc/ha.d/
[root@node1 ha.d]# vim authkeys
auth 3
3 md5 Hello!
[root@node1 ha.d]# vim ha.cf
bcast eth1
node node1
node node2
[root@node1 ha.d]# vim haresources
node1 IPaddr::192.168.1.100/24/eth0 Filesystem::192.168.1.105:/mysql/data::/var/lib/mysql::nfs mysqld
[root@node1 ha.d]#
6.创建mysql远程连接帐号
node1节点:
[root@node1 ha.d]# /usr/local/mysql-5.7.4/bin/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.7.4-m14 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> grant all privileges on *.* to 'remote'@'%' identified by 'system'; --创建远程帐号
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@node1 ha.d]# /usr/local/mysql-5.7.4/bin/mysql -u remote -p -h 192.168.1.100
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.4-m14 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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 tong;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tong |
+--------------------+
4 rows in set (0.01 sec)
mysql>
[root@node2 ~]# /usr/local/mysql-5.7.4/bin/mysql -u remote -p -h 192.168.1.100
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.22 Source distribution
Copyright (c) 2000, 2014, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tong |
+--------------------+
4 rows in set (0.03 sec)
mysql>