MySQL二进制安装与数据库全量备份

版权声明:未经本人允许严禁转载 https://blog.csdn.net/WanJiaBaoBao/article/details/82842155

搭建要求

  • 下载MySQL二进制格式包并安装使用;
  • 创建test数据库,在test数据库中创建student表,表结构如下:
id int(11) not null name varchar(100) not null age tinyint(4) null
1 tom 20
2 jerry 23
3 wangqing 25
4 sean 28
5 zhangshan 26
6 zhangshan 20
7 lisi null
8 chenshuo 10
9 wangwu 3
10 qiuyi 15
11 qiuxiaotian 20
  • 备份数据库和表

搭建环境


搭建步骤

  1. 安装MySQL
## 先创建mysql的用户和组 ##
[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# useradd -r -M -s /sbin/nologin -g 306 mysql

## 下载MySQL二进制包并使用 ##
[root@localhost ~]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

## 下载完成后进行解压,将解压的文件指定到/usr/local/目录下 ##
[root@localhost ~]# tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

## 解压完成,进入目录,创建一个软链接,将软链接的属主和属组改为
[root@localhost ~]# cd /usr/local
[root@localhost local]# ln -s mysql-5.7.22-linux-glibc2.12-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql

## 可以将/usr/local/mysql/bin目录,写到环境变量里面 ##
[root@localhost bin]# vi /etc/profile.d/mysql
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# source /etc/profile.d/mysql
[root@localhost mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

## 建立数据库存放位置,并将属主和属组改为mysql ##
[root@localhost mysql]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data

## 初始化数据库 ##
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data
2018-09-25T18:20:26.662587Z 1 [Note] A temporary password is generated for root@localhost: 6<&_o/la,U5k
注:初始登录密码为: 6<&_o/la,U5k

## 生成配置文件 ##
[root@localhost ~]# cat > /etc/my.cnf << EOF
> [mysqld]
> basedir=/usr/local/mysql
> datadir=/opt/data
> socket=/tmp/mysql.sock
> port=3306
> pid-file=/opt/data/mysql.pid
> user=mysql
> skip-name-resolve
> EOF

## 配置启动服务脚本,并修改启动脚本参数 ##
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# vi /etc/init.d/mysqld
找到以下两行:
basedir=
datadir=
修改为:
basedir=/usr/local/mysql  #指定mysql的安装路径
datadir=/opt/data   #指定数据库存放路径

## 启动mysql服务,并查看监听端口 ##
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
 [root@localhost ~]# ss -ntlp
 LISTEN     0      80      :::3306       :::*      users:(("mysqld",9828,20))

## 登录到数据库,修改账户密码 ##
[root@localhost ~]# mysql -uroot -p
Enter password:        #输入初始密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password=password('123456');     #将当前用户的密码修改为“123456”

## 退出数据库,使用新密码重新登录数据库 ##
[root@localhost ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

  1. 创建数据库和表
## 登录MySQL数据库,创建数据库,表创建表,并查看结构 ##
mysql> create database test;     #创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> use test;      #使用数据库
Database changed
mysql> create table student(id int(11) not null,name varchar(100) not null,age tinyint(11) null);   #创建表
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;     #查看表结构
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | NO   |     | NULL    |       |
| age   | tinyint(11)  | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.03 sec)

## 向表中插入数据,并查看表内容 ##
mysql> insert into student values(1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),
    -> (5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),
    -> (10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0
mysql> select * from student;     #查询表内容
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

  1. 备份数据库和表
## 备份全部数据库,将备份的数据库命名为all-db-201809251927.sql ##
[root@localhost ~]# mysqldump -uroot -p --all-databases > all-db-201809251927.sql
Enter password:

## 备份一个或者多个数据库,将创建的数据库进行备份,命名为test-db-201809251928.sql ##
[root@localhost ~]# mysqldump -uroot -p --databases test > test-db-201809251928.sql
Enter password:

## 备份表,将添加的student表进行备份,命名为student-table-201809251930.sql ##
[root@localhost ~]# mysqldump -uroot -p test student > student-table-201809251930.sql
Enter password:

## 若要将用户名和密码省略,则需要修改"~/.my.cnf"配置文件 ##
[root@localhost ~]# vi .my.cnf
[mysqldump]
user=root
password=123456
[root@localhost ~]# mysqldump test student > student-table-201809251948.sql

  1. 恢复数据
## 登录数据库若不想输入用户和密码,则需要修改“~/.my.cnf”配置文件 ##
[root@localhost ~]# vi .my.cnf
[mysql]
user=root
password=123456
[root@localhost ~]# mysql   #登录数据库
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 


## 若表丢失,将备份的表用来恢复 ##
mysql> delete from test.student;     #删除数据库中的表,表数据丢失
Query OK, 11 rows affected (0.01 sec)
mysql> select * from test.student;     #查看表中数据,没有数据
Empty set (0.00 sec)

[root@localhost ~]# mysql test < student-table-201809251930.sql    #恢复表到test数据库中

## 查看数据库中表是否恢复 ##
[root@localhost ~]# mysql -e "select * from test.student"    #登录数据库执行查询表命令,可以看到表内容已经恢复
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+

出现的错误

## 错误一:
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

## 解决办法:查找libaio.so.1库的安装包,安装libaio包。
[root@localhost ~]# yum provides libaio.so.1
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
yum.repo                                                                 | 4.1 kB  00:00:00     
(1/2): yum.repo/group_gz                                                 | 137 kB  00:00:00     
(2/2): yum.repo/primary_db                                               | 4.0 MB  00:00:00     
libaio-0.3.109-13.el7.i686 : Linux-native asynchronous I/O access library
Repo        : yum.repo
Matched from:
Provides    : libaio.so.1

[root@localhost ~]# yum -y install libaio

猜你喜欢

转载自blog.csdn.net/WanJiaBaoBao/article/details/82842155