MySQL安装及数据备份和恢复

MySQL二进制格式安装

  • 首先下载mysql二进制安装包
//下载地址
[root@hxdserver src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz


//创建用户和组
[root@hxdserver ~]# groupadd -r mysql
[root@hxdserver ~]# useradd -M -s /sbin/nologin -g mysql mysql
//解压之前下载好的安装包
[root@hxdserver ~]# cd /usr/local/
[root@hxdserver local]# ls
bin  games    lib    libexec                                     sbin   src
etc  include  lib64  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  share
[root@hxdserver local]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 
[root@hxdserver local]# ls
bin    include  libexec                                     sbin
etc    lib      mysql-5.7.23-linux-glibc2.12-x86_64         share
games  lib64    mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  src
//创建一个软连接,方便使用
[root@hxdserver local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql 
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"
[root@hxdserver local]# ll
lrwxrwxrwx. 1 root root        36 9月  25 17:24 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root       129 9月  25 17:19 mysql-5.7.23-linux-glibc2.12-x86_64
//修改目录的属主和属组
[root@hxdserver ~]# chown -R mysql.mysql /usr/local/mysql
[root@hxdserver ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月  25 17:24 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
//添加环境变量
[root@hxdserver ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@hxdserver ~]# . /etc/pro
profile    profile.d/ protocols  
[root@hxdserver ~]# . /etc/pro
profile    profile.d/ protocols  
[root@hxdserver ~]# . /etc/profile.d/mysql.sh 
[root@hxdserver ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//创建数据储存的目录
[root@hxdserver ~]# mkdir /opt/data
[root@hxdserver ~]# chown -R mysql.mysql /opt/data/
[root@hxdserver ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月  25 17:35 data
//初始化数据库
[root@hxdserver ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data
2018-09-25T10:22:38.855635Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-25T10:22:39.512697Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-25T10:22:39.553504Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-25T10:22:39.640246Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ee1a32a6-c0ac-11e8-be86-000c294b7cb8.
2018-09-25T10:22:39.641568Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-25T10:22:39.647861Z 1 [Note] A temporary password is generated for root@localhost: 08waBnuLEe(>   //这里冒号后面的是初始密码
//生成配置文件
[root@hxdserver ~]# vim /etc/my.cnf
[root@hxdserver ~]# cat /etc/my.cnf
[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
//配置服务启动脚本
[root@hxdserver ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld[root@hxdserver ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld 
[root@hxdserver ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld 
//启动mysql
[root@hxdserver ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/hxdserver.err'.
. SUCCESS! 
[root@hxdserver ~]# ps -ef |grep mysql
root      13436      1  0 18:44 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     13614  13436  2 18:44 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hxdserver.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      13648  12759  0 18:44 pts/1    00:00:00 grep --color=auto mysql
[root@hxdserver ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:22                           *:*                  
LISTEN     0      100      127.0.0.1:25                           *:*                  
LISTEN     0      128             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      80              :::3306                        :::*   

//修改密码,登录时使用前面所给的初始密码
[root@hxdserver ~]# /usr/local/mysql/bin/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.23

Copyright (c) 2000, 2018, 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> 
//设置新密码
mysql> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)


  • 数据库的基本操作及备份和恢复
//创建数据库和表,并向表中添加内容
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database hxd;
Query OK, 1 row affected (0.00 sec)

mysql> use hxd;
Database changed
mysql> create table student(id int not null,name varchar(20),age tinyint);
Query OK, 0 rows affected (0.04 sec)

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | tinyint(4)  | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> create table dubai(id int not null,name varchar(50),age tinyint);
Query OK, 0 rows affected (0.00 sec)

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | tinyint(4)  | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc dubai;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| age   | tinyint(4)  | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into student(id,name,age)values(1,'zhangsan',5),(2,'lisi',10),(3,'wangwu',15),(4,'zhaoliu',20);
Query OK, 4 rows affected (0.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into dubai(id,name,age)values(1,'tom',5),(2,'jerry',10);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> show tables;
+---------------+
| Tables_in_hxd |
+---------------+
| dubai         |
| student       |
+---------------+
2 rows in set (0.00 sec)

mysql> 

  • 备份数据库
//这里我只演示被分到当前主机中,备份到其他主机就如法炮制即可

//备份所有数据库
[root@hxdserver ~]# mysqldump -u root -p --all-databases > all-201809251900.sql
Enter password: 
[root@hxdserver ~]# ls
all-201809251900.sql  anaconda-ks.cfg
//备份hxd库的student表和dubai表
[root@hxdserver ~]# mysqldump -u root -p hxd student dubai > table-201809251900.sql
Enter password: 
[root@hxdserver ~]# ls
all-201809251900.sql  anaconda-ks.cfg  table-201809251900.sql
//备份hxd库
[root@hxdserver ~]# mysqldump -u root -p --databases hxd > hxd-201809251900.sql
Enter password: 
[root@hxdserver ~]# ls
all-201809251900.sql  anaconda-ks.cfg  hxd-201809251900.sql  table-201809251900.sql

//模拟误删除hxd数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hxd                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database hxd;
Query OK, 2 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
//恢复hxd库
[root@hxdserver ~]# mysql -u root -p < all-201809251900.sql
Enter password: 
[root@hxdserver ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 |
| hxd                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
//模拟误删除整个数据库后恢复
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hxd                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database hxd;
Query OK, 2 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[root@hxdserver ~]# mysql -u root -p < all-201809251900.sql
Enter password: 
[root@hxdserver ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 |
| hxd                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)


猜你喜欢

转载自blog.csdn.net/dubaiToT/article/details/82843610