mysql的二进制安装和数据库备份

1.mysql的二进制安装

mysql的包非常大,使用wget下载可能要很久,如果很慢的话可在百度云去下载链接: https://pan.baidu.com/s/1ZYXEfcLkpP8F7uyxtajC3w 提取码: ct7j

1.1下载mysql包

[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/my
sql-5.7.22-linux-glibc2.12-x86_64.tar.gz

1.2创建mysql的用户和组

[root@localhost ~]# groupadd -r kongcheng
[root@localhost ~]# useradd -M -s /sbin/nologin -g kongcheng kongcheng

1.3解压mysql包

[root@localhost ~]# tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/      mysql         创建软连接
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"  
[root@localhost local]# ll
lrwxrwxrwx  1 root root  36 9月  25 16:43 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 129 9月  25 16:38 mysql-5.7.23-linux-glibc2.12-x86_64

1.4修改解压位置mysql文件的属主和组

[root@localhost local]# chown -R kongcheng.kongcheng mysql
[root@localhost local]# ll
lrwxrwxrwx  1 kongcheng kongcheng  36 9月  25 16:43 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

1.5创建环境变量

[root@localhost mysql]# cd /etc/profile.d/
[root@localhost profile.d]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost profile.d]# chmod +x mysql.sh 
[root@localhost profile.d]# ./mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql

1.6建立数据存放目录并初始化

[root@localhost ~]# mkdir /kongbai
[root@localhost ~]# chown -R kongcheng.kongcheng /kongbai/
[root@localhost ~]# ll /
drwxr-xr-x    2 kongcheng kongcheng    6 9月  25 16:52 kongbai
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=kongcheng --datadir=/kongbai/
2018-09-25T09:03:09.009002Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-25T09:03:10.126793Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-25T09:03:10.270192Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-25T09:03:10.354322Z 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: d362eb48-c0a1-11e8-b6cd-000c29524561.
2018-09-25T09:03:10.355219Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-25T09:03:10.407502Z 1 [Note] A temporary password is generated for root@localhost: N_gRsykpO1wx   生成临时密码

1.7生成配置文件

[root@localhost ~]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /kongbai
socket = /tmp/mysql.sock
port = 3306
pid-file = /kongbai/mysql.pid
user = kongcheng
skip-name-resolve

1.8配置服务启动脚本

[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost support-files]# vim mysql.server
basedir=/usr/lical/mysql
datadir=/kongbai    修改这两行

1.9启动服务mysql

[root@localhost ~]#  /etc/init.d/mysqld start
Starting MySQL.Logging to '/kongbai/localhost.localdomain.err'.
. SUCCESS! 

1.10修改密码

[root@localhost ~]# mysql -uroot -p
Enter password:     使用前面的临时密码登陆
mysql> set password = password('wscl1996.');
Query OK, 0 rows affected, 1 warning (0.00 sec)

2.mysql数据库备份

2.1查看数据库内容

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chengli            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_chengli |
+-------------------+
| shudent           |
+-------------------+
1 row in set (0.00 sec)

2.2 备份表和库

[root@localhost ~]# mysqldump -uroot -p chengli shudent > shudent-table-201809281721.sql
Enter password:    输入修改后的密码
[root@localhost ~]# mysqldump -uroot -p databases chengli  > chengli-database-201809281722.sql
Enter password: 
[root@localhost ~]# ll
总用量 629316
-rw-------. 1 root root      1784 9月  19 14:39 anaconda-ks.cfg
-rw-r--r--  1 root root      2054 9月  25 17:22 chengli-database-201809281722.sql
-rw-r--r--. 1 root root      1832 9月  19 14:40 initial-setup-ks.cfg
-rw-r--r--  1 root root 644399365 9月   5 23:24 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--  1 root root      2054 9月  25 17:21 shudent-table-201809281721.sql

2.3删除表

mysql> use chengli;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table shudent;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.00 sec)

2.4还原表

[root@localhost ~]# mysql -uroot -p chengli < shudent-table-201809281721.sql
Enter password: 
mysql> show tables;
+-------------------+
| Tables_in_chengli |
+-------------------+
| shudent           |
+-------------------+
1 row in set (0.00 sec)

2.5删除库

mysql> drop database chengli;
Query OK, 1 row affected (0.23 sec)

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

2.6还原库

[root@localhost ~]# mysql -uroot -p < chengli-database-201809281722.sql
Enter password: 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chengli            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/Empty_city_dreams/article/details/82842410