二進位制格式MySQL的安裝、備份與恢復
阿新 • • 發佈:2018-12-16
環境說明:
主機名 | 系統 | 服務 |
---|---|---|
server | red7 | mysql |
需求:一個MySQL包。例:mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
二進位制格式安裝MySQL
[[email protected] ~]# ls
anaconda-ks.cfg mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[[email protected] ~]# tar -xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# groupadd -r mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql
[[email protected] ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.23-linux-glibc2.12-x86_64 share
[ [email protected] ~]# cd /usr/local/
[[email protected] local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"
[[email protected] 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
[ [email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] 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
[[email protected] local]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[[email protected] local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] local]# . /etc/profile.d/mysql.sh
[[email protected] local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] local]# cd
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 9月 17 11:50 data
[[email protected] ~]# /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 [email protected]: xFH7QL.5(FM1
[[email protected] ~]# echo 'xFH7QL.5(FM1' > pass
[[email protected] ~]# cat pass
xFH7QL.5(FM1
[[email protected] ~]# 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
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# 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 //新增路徑
[[email protected] ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/server.err'.
SUCCESS!
[[email protected] ~]# 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
[[email protected] ~]# 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 :::*
[[email protected] ~]# 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
[[email protected] ~]# 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>
備份
[[email protected] ~]# ls
anaconda-ks.cfg mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz pass
[[email protected] ~]# mysqldump -uroot -p --all-databases > all-201809261832.sql
Enter password:
[[email protected] ~]# ls
all-201809261832.sql mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg pass
[[email protected] ~]# mysqldump -uroot -p ls student > ls-201809261843.sql
Enter password:
[[email protected] ~]# ls
all-201809261832.sql student-201809261843.sql pass
anaconda-ks.cfg mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[[email protected] ~]# mysqldump -uroot -p --databases ls > ls-201809261847.sql
Enter password:
[[email protected] ~]# 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資料庫
[[email protected] ~]# 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資料庫
[[email protected] ~]# mysql -uroot -p < ls-201809261847.sql
Enter password:
[[email protected] ~]# mysql -uroot -p -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| ls |
| mysql |
| performance_schema |
| sys |
+--------------------+
[[email protected] ~]#
恢復表
mysql> use ls;
Database changed
mysql> source student-201809261843.sql
Query OK, 0 rows affected (0.00 sec)
...
mysql>
模擬誤刪整個資料庫
[[email protected] ~]# 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
恢復整個資料庫
[[email protected] ~]# 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
[[email protected] ~]# mysql -uroot -p < all-201809261832.sql
Enter password:
[[email protected] ~]# mysql -uroot -p -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| ls |
| mysql |
| performance_schema |
| sys |
+--------------------+