假设: centos服务器AB两台,A上的生产MySQL出现故障不能启动,但是数据文件是完好无损的。现在需要在B服务器上恢复数据库。
开始模拟
准备两台centos7虚拟机
192.168.27.136 A
192.168.27.136 B
安装mysql数据库
[root@localhost ~]# yum -y install mariadb mariadb-server
A上操作
1. 启动数据库 创建库表并插入数据(写点数据)
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database www character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| www |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use www;
Database changed
MariaDB [www]> create table biao(id int(5),name varchar(10));
Query OK, 0 rows affected (0.00 sec)
MariaDB [www]> insert into biao values(1,"小红");
Query OK, 1 row affected (0.00 sec)
MariaDB [www]> select * from biao;
+------+--------+
| id | name |
+------+--------+
| 1 | 小红 |
+------+--------+
1 row in set (0.00 sec)
2.关掉数据库 打包数据
[root@localhost ~]# systemctl stop mariadb
[root@localhost lib]# cd /var/lib/
[root@localhost lib]# tar czvf mysql.tar.gz mysql
3.使用ssh copy到要备份的服务器
[root@localhost lib]# scp mysql.tar.gz 192.168.27.137:/usr/local/
[email protected]'s password:
mysql.tar.gz 100% 572KB 66.6MB/s 00:00
B.上操作
1.解压数据包,给予属主属组mysql
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar xzf mysql.tar.gz
[root@localhost local]# chown -R mysql.mysql mysql
2.修改mysql配置文件datadir目录 (这里的)
[root@localhost local]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[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.重启mysql 查看数据有没有备份
[root@localhost local]# systemctl restart mariadb
[root@localhost local]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, 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 |
| www |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use www;
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 [www]> select * from biao;
+------+--------+
| id | name |
+------+--------+
| 1 | 小红 |
+------+--------+
1 row in set (0.00 sec)