实验环境:两台服务器IP:192.168.1.117;IP:192.168.1.118.
实验目的:实现数据库横向扩展。
实验方案:服务器192.168.1.117作为主节点,服务器192.168.1.118.作为从节点。
实验步骤:分为五步来完成实验,首先数据库安装,其次主从节点配置,再次复制功能实现,最后做测试。
一、数据库的安装
在此安装mariadb-5.5.42-linux-x86_64版本。
[[email protected] ~]# mkdir -pv /mydata/data //创建目录,做数据存储位置。实际应用中应挂硬盘。
mkdir: created directory `/mydata‘
mkdir: created directory `/mydata/data‘
[[email protected] ~]# useradd mysql //创建mysql用户。
[[email protected] ~]# chown -R mysql.mysql /mydata/data
[[email protected] mariadb]# ls
mariadb-5.5.42-linux-x86_64.tar.gz
[[email protected] mariadb]# tar xf mariadb-5.5.42-linux-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local
[[email protected] local]# ln -sv mariadb-5.5.42-linux-x86_64 mysql //解压之后,创建链接文件。
`mysql‘ -> `mariadb-5.5.42-linux-x86_64‘
[[email protected] local]# cd mysql
[[email protected] mysql]# chown -R root.mysql ./*
[[email protected] mysql]# mkdir /etc/mysql //在此创建目录放MySQL的配置文件。
[[email protected] mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf //mysql的配置文件。
[[email protected] mysql]# vim /etc/mysql/my.cnf
datadir=/mydata/data
[[email protected] mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
cp: overwrite `/etc/rc.d/init.d/mysqld‘? yes
[[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld
[[email protected] mysql]# scripts/mysql_install_db --datadir=/mydata/data --user=mysql//数据库的初始化。
[[email protected] mysql]# mkdir -pv /mydata/{binlogs,relaylogs} //二进制日志和中继日志存放位置。
mkdir: created directory `/mydata/binlogs‘
mkdir: created directory `/mydata/relaylogs‘
[[email protected] mysql]#
[[email protected] mysql]# chown -R mysql.mysql /mydata/ //为/mydata/目录下的所有文件定义成mysql用户mysql组。
[[email protected] mysql]# vim /etc/profile.d/mysql.sh //配置mysql的PATH路径。
export PATH=/usr/local/mysql/bin:$PATH
[[email protected] mysql]# source /etc/profile.d/mysql.sh //到此处mariadb的安装基本完成。
二、数据库的主从节点配置
[[email protected] mysql]# vim /etc/mysql/my.cnf //在此定义主节点,此时切换到
log-bin=/mydata/binlogs/mysql-master-bin
binlog_format=mixed
server-id = 1
[[email protected] mysql]# vim /etc/mysql/my.cnf //在此定义从节点。
relay-log=/mydata/relaylogs/mysql-relay-log
# log-bin=mysql-bin
# binlog_format=mixed
server-id = 11
[[email protected] mysql]# service mysqld start
Starting MySQL..... [ OK ]
[[email protected] mysql]# mysql //启动之后即可以
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.42-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]>
三、数据库的复制功能实现
在主节点上,创建有复制权限的账号。
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO [email protected]‘192.168.1.118‘ IDENTIFIED BY ‘repass‘;
Query OK, 0 rows affected (0.06 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.12 sec)
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-master-bin.000002 | 745 | | |
+-------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在从节点上使用有复制权限的账号链接master。
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.1.117‘,MASTER_USER=‘reuser‘,MASTER_PASSWORD=‘repass‘,MASTER_LOG_FILE=‘mysql-master-bin.000002‘,MASTER_LOG_POS=745;
Query OK, 0 rows affected (0.22 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G; //下面只是一部分。
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.117
Master_User: reuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000002
Read_Master_Log_Pos: 745
Relay_Log_File: mysql-relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-master-bin.000002
Slave_IO_Running: No
Slave_SQL_Running: No //io线程和sql线程均未启动。
在从节点数据io线程和sql线程的开启。
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.12 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G; //再次查看已开启。
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.117
Master_User: reuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000002
Read_Master_Log_Pos: 832
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 623
Relay_Master_Log_File: mysql-master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
四、做下测试,现在主节点上创建一个数据库,而后查看从节点上数据库
MariaDB [(none)]> CREATE DATABASE dbtest;
Query OK, 1 row affected (0.05 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbtest |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.06 sec)
MariaDB [(none)]>
MariaDB [(none)]> SHOW DATABASES; //在从节点上查看数据库信息,很明显实验成功了。
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbtest |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.30 sec)
MariaDB [(none)]>