1. 程式人生 > >MySQL安裝及資料備份和恢復

MySQL安裝及資料備份和恢復

MySQL二進位制格式安裝

  • 首先下載mysql二進位制安裝包
//下載地址
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz


//建立使用者和組
[[email protected] ~]# groupadd -r mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql
//解壓之前下載好的安裝包
[
[email protected]
~]# cd /usr/local/ [[email protected] local]# ls bin games lib libexec sbin src etc include lib64 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz share [[email protected] local]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz [
[email protected]
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 //建立一個軟連線,方便使用 [[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 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 //修改目錄的屬主和屬組 [[email protected] ~]# chown -R mysql.mysql /usr/local/mysql [[email protected] ~]# 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/ //新增環境變數 [[email protected] ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [[email protected] ~]# . /etc/pro profile profile.d/ protocols [[email protected] ~]# . /etc/pro profile profile.d/ protocols [[email protected] ~]# . /etc/profile.d/mysql.sh [[email protected] ~]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin //建立資料儲存的目錄 [[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月 25 17:35 data //初始化資料庫 [[email protected] ~]# /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 [email protected]: 08waBnuLEe(> //這裡冒號後面的是初始密碼 //生成配置檔案 [[email protected] ~]# vim /etc/my.cnf [[email protected] ~]# 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 //配置服務啟動指令碼 [[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld[[email protected] ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld [[email protected] ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld //啟動mysql [[email protected] ~]# /etc/init.d/mysqld start Starting MySQL.Logging to '/opt/data/hxdserver.err'. . SUCCESS! [[email protected] ~]# 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 [[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] ~]# /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> 

  • 備份資料庫
//這裡我只演示被分到當前主機中,備份到其他主機就如法炮製即可

//備份所有資料庫
[[email protected] ~]# mysqldump -u root -p --all-databases > all-201809251900.sql
Enter password: 
[[email protected] ~]# ls
all-201809251900.sql  anaconda-ks.cfg
//備份hxd庫的student表和dubai表
[[email protected] ~]# mysqldump -u root -p hxd student dubai > table-201809251900.sql
Enter password: 
[[email protected] ~]# ls
all-201809251900.sql  anaconda-ks.cfg  table-201809251900.sql
//備份hxd庫
[[email protected] ~]# mysqldump -u root -p --databases hxd > hxd-201809251900.sql
Enter password: 
[[email protected] ~]# 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庫
[[email protected] ~]# mysql -u root -p < all-201809251900.sql
Enter password: 
[[email protected] ~]# 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
[[email protected] ~]# mysql -u root -p < all-201809251900.sql
Enter password: 
[[email protected] ~]# 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)