二进制格式MySQL的安装、备份与恢复

版权声明:如需转载请私信我,经我同意才可转载,转载需附上原地址。 https://blog.csdn.net/qq_26553835/article/details/82858001

环境说明:

主机名 系统 服务
server red7 mysql

需求:一个MySQL包。例:mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

二进制格式安装MySQL

[root@server ~]# ls
anaconda-ks.cfg  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@server ~]# tar -xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@server ~]# groupadd -r mysql
[root@server ~]# useradd -M -s /sbin/nologin -g mysql mysql
[root@server ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.23-linux-glibc2.12-x86_64  share
[root@server ~]# cd /usr/local/
[root@server local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"
[root@server local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 3月  10 2016 bin
drwxr-xr-x. 2 root root   6 3月  10 2016 etc
drwxr-xr-x. 2 root root   6 3月  10 2016 games
drwxr-xr-x. 2 root root   6 3月  10 2016 include
drwxr-xr-x. 2 root root   6 3月  10 2016 lib
drwxr-xr-x. 2 root root   6 3月  10 2016 lib64
drwxr-xr-x. 2 root root   6 3月  10 2016 libexec
lrwxrwxrwx. 1 root root  36 9月  17 11:45 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 9月  17 11:39 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 3月  10 2016 sbin
drwxr-xr-x. 5 root root  49 9月  12 17:15 share
drwxr-xr-x. 2 root root   6 3月  10 2016 src
[root@server local]# chown -R mysql.mysql /usr/local/mysql
[root@server local]# ll
总用量 0
drwxr-xr-x. 2 root  root    6 3月  10 2016 bin
drwxr-xr-x. 2 root  root    6 3月  10 2016 etc
drwxr-xr-x. 2 root  root    6 3月  10 2016 games
drwxr-xr-x. 2 root  root    6 3月  10 2016 include
drwxr-xr-x. 2 root  root    6 3月  10 2016 lib
drwxr-xr-x. 2 root  root    6 3月  10 2016 lib64
drwxr-xr-x. 2 root  root    6 3月  10 2016 libexec
lrwxrwxrwx. 1 mysql mysql  36 9月  17 11:45 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root  root  129 9月  17 11:39 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 3月  10 2016 sbin
drwxr-xr-x. 5 root  root   49 9月  12 17:15 share
drwxr-xr-x. 2 root  root    6 3月  10 2016 src
[root@server local]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@server local]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh 
[root@server local]# . /etc/profile.d/mysql.sh 
[root@server local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server local]# cd
[root@server ~]# mkdir /opt/data
[root@server ~]# chown -R mysql.mysql /opt/data/
[root@server ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月  17 11:50 data
[root@server ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-17T03:52:15.818377Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-17T03:52:18.303011Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-17T03:52:18.631444Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-17T03:52:19.203706Z 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: 13212e28-ba2d-11e8-a31c-000c2938489e.
2018-09-17T03:52:19.227845Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-17T03:52:19.229183Z 1 [Note] A temporary password is generated for root@localhost: xFH7QL.5(FM1
[root@server ~]# echo 'xFH7QL.5(FM1' > pass
[root@server ~]# cat pass 
xFH7QL.5(FM1
[root@server ~]# 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@server ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@server ~]# vim /etc/init.d/mysqld 
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql                      //添加路径
datadir=/opt/data                             //添加路径
[root@server ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/server.err'.
 SUCCESS! 
[root@server ~]# ps -ef | grep mysql
root      12929      1  0 12:01 pts/2    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     13107  12929  1 12:01 pts/2    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=server.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      13139   2921  0 12:01 pts/2    00:00:00 grep --color=auto mysql
[root@server ~]# 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@server ~]# 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> set password = password('lslsls123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit;
Bye
[root@server ~]# mysql -uroot -plslsls123!
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.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> 

备份

[root@server ~]# ls
anaconda-ks.cfg  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  pass
[root@server ~]# mysqldump -uroot -p --all-databases > all-201809261832.sql
Enter password: 
[root@server ~]# ls
all-201809261832.sql  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg       pass
[root@server ~]# mysqldump -uroot -p ls student > ls-201809261843.sql
Enter password: 
[root@server ~]# ls
all-201809261832.sql  student-201809261843.sql                         pass
anaconda-ks.cfg       mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@server ~]# mysqldump -uroot -p --databases ls > ls-201809261847.sql
Enter password: 
[root@server ~]# ls
all-201809261832.sql  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg       pass
ls-201809261847.sql   student-201809261843.sql
模拟误删ls数据库
[root@server ~]# mysql -uroot -plslsls123!
mysql> drop database ls;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
mysql> quit
Bye
恢复ls数据库
[root@server ~]# mysql -uroot -p < ls-201809261847.sql 
Enter password: 
[root@server ~]# mysql -uroot -p -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ls                 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@server ~]# 

恢复表
mysql> use ls;
Database changed
mysql> source student-201809261843.sql
Query OK, 0 rows affected (0.00 sec)
...


mysql> 

模拟误删整个数据库
[root@server ~]# mysql -uroot -plslsls123!
mysql> drop database ls;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
mysql> quit
Bye
恢复整个数据库
[root@server ~]# ls
all-201809261832.sql  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg       pass
ls-201809261847.sql   student-201809261843.sql
[root@server ~]# mysql -uroot -p < all-201809261832.sql 
Enter password: 
[root@server ~]# mysql -uroot -p -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ls                 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

猜你喜欢

转载自blog.csdn.net/qq_26553835/article/details/82858001