准备:配置yum源,两个网卡(例如:192.168.223.30和192.168.223.40),镜像使用提供的CentOS-7-x86_64-DVD-1511.iso。虚拟机配置为1核/2G内存/20G硬盘
一.基础环境配置
1.修改主机名
[root@localhost ~]# hostnamectl set-hostname mysql1 [root@mysql1 ~]# bash [root@localhost ~]# hostnamectl set-hostname mysql2 [root@mysql2 ~]# bash
2.关闭防火墙
[root@mysql1 ~]# setenforce 0 [root@mysql1 ~]# systemctl stop firewalld [root@mysql2 ~]# setenforce 0 [root@mysql2 ~]# systemctl stop firewalld
3.修改hosts文件
在两个节点均配置/etc/hosts,命令如下
[root@mysql1 ~]# vi /etc/hosts [root@mysql2 ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.223.30 mysql1(新增) 192.168.223.40 mysql2(新增)
4.安装数据库服务
[root@mysql1 ~]# yum install -y mariadb mariadb-server [root@mysql2 ~]# yum install -y mariadb mariadb-server 启动数据库服务并设置开机自启 [root@mysql1 ~]# systemctl start mariadb [root@mysql1 ~]# systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. [root@mysql2 ~]# systemctl start mariadb [root@mysql2 ~]# systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
二、初始化数据库并配置主从服务
1、初始化数据库
在两个节点均初始化,设置密码(例如123456)
[root@mysql1 ~]# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we’ll need the current password for the root user. If you’ve just installed MariaDB, and you haven’t set the root password yet, the password will be blank, so you should just press enter here.
Enter current password for root (enter for none): #默认按回车 OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] y New password: #输入数据库root密码123456 Re-enter new password: #再次输入密码123456 Password updated successfully! Reloading privilege tables.. … Success!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n] y … Success!
Normally, root should only be allowed to connect from ‘localhost’. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n … skipping.
By default, MariaDB comes with a database named ‘test’ that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database… … Success!
- Removing privileges on test database… … Success!
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? [Y/n] y … Success!
Cleaning up…
All done! If you’ve completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!
[root@mysql2 ~]# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we’ll need the current password for the root user. If you’ve just installed MariaDB, and you haven’t set the root password yet, the password will be blank, so you should just press enter here.
Enter current password for root (enter for none): #默认按回车 OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] y New password: #输入数据库root密码123456 Re-enter new password: #再次输入密码123456 Password updated successfully! Reloading privilege tables.. … Success!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n] y … Success!
Normally, root should only be allowed to connect from ‘localhost’. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n … skipping.
By default, MariaDB comes with a database named ‘test’ that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database… … Success!
- Removing privileges on test database… … Success!
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? [Y/n] y … Success!
Cleaning up…
All done! If you’ve completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!
2.配置mysql节点
修改两个节点的数据库配置文件,在/etc/my.cnf中添加如下内容
配置mysql1主节点
[root@mysql1 ~]# vi /etc/my.cnf [root@mysql1 ~]# cat /etc/my.cnf [mysqld] log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 30
datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0[mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid
#
include all files from the config directory
# !includedir /etc/my.cnf.d
配置mysql2从节点
[root@mysql2 ~]# vi /etc/my.cnf [root@mysql2 ~]# cat /etc/my.cnf [mysqld] log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 30
datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0[mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid
#
include all files from the config directory
# !includedir /etc/my.cnf.d
3.重启数据库服务
[root@mysql1 ~]# systemctl restart mariadb [root@mysql2 ~]# systemctl restart mariadb
4.进入数据库
[root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.44-MariaDB-log 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)]> grant all privileges on . to root@’%’ identified by “123456”; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant replication slave on . to ‘user’@’mysql2’ identified by ‘123456’; Query OK, 0 rows affected (0.00 sec)
[root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 5.5.44-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)]> change master to master_host=’mysql1’,master_user=’user’,master_password=’123456’; Query OK, 0 rows affected (0.01 sec)
配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。
MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G * 1. row * Slave_IO_State: Waiting for master to send event Master_Host: mysql1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 245 Relay_Log_File: mariadb-relay-bin.000005 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 245 Relay_Log_Space: 1256 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 30 1 row in set (0.00 sec)
三、验证数据库主从服务是否成功开启
1.创建主节点数据库
[root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.44-MariaDB-log 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)]> create database test; Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use test; Database changed MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> insert into company values(1,”alibaba”,”china”); Query OK, 1 row affected (0.01 sec)
MariaDB [test]> select * from company; +——+————-+———-+ | id | name | addr | +——+————-+———-+ | 1 | alibaba | china | +——+————-+———-+ 1 row in set (0.00 sec)
2.从节点验证数据库复制功能
[root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 5.5.44-MariaDB-log 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)]> show databases; +——————————+ | Database | +——————————+ | information_schema | | mysql | | performance_schema | | test | +——————————+ 4 rows in set (0.00 sec)
MariaDB [(none)]> use test; 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 MariaDB [test]> show tables; +————————+ | Tables_in_test | +————————+ | company | +————————+ 1 row in set (0.00 sec)
MariaDB [test]> select * from company; +——+————-+———-+ | id | name | addr | +——+————-+———-+ | 1 | alibaba | china | +——+————-+———-+ 1 row in set (0.00 sec)
数据库服务成功启动运行。