mysql进阶简单解析

mysql进阶

1.mysql配置文件

mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FI
  LE --> ~/.my.cnf

mysql常用配置文件参数:

参数 说明
port 设置监听端口
socket=/tmp/mysql.sock 指定套接字文件位置
basedir=/usr/local/mysql 指定mysql数据存放的路径
pid-file=/data/mysql/mysql.pid 指定进程ID文件存放路径
user=mysql 指定MySQL以什么身份来提供服务
skip-name-resolve 禁止MySQL对外部连接进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则MySQL将无法正常处理连接请求

2.mysql数据库备份

2.1 数据库常用备份方案

数据库备份方案:

  • 全量备份
  • 增量备份
  • 差异备份
备份方案 特点
差异备份 备份上一次的完全备份后发生变化的所有文件。 差异备份是指在一次备份后到进行差异备份的这段时间内,对那些增加或修改文件的备份。进行恢复时,我们只需要对第一次全量备份和最后一次差异备份进行恢复。
全量备份 全量备份就是指对某一个时间点上吃所有数据或应用进行的一个完全拷贝。 数据恢复快备份时间长
增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次备份只需要备份与前一次相比增加或者被修改的文件。这就意味着,第一次增量备份的对象是进行一次备份后产生的增加和修改的文件,如此类推。 没有重复的备份数据备份时短恢复数据时必须按一定的顺序进行

2.2mysql备份工具mysqldump

//语法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]

常用的OPTIONS:

-uUSERNAME  //指定数据库用户名
-hHOST     //指定服务器主机名,请使用ip地址
-pPASSWORD  //指定数据库用户的密码
-P#         //指定数据库监听的端口,这里的#需要用实际的端口号代替

备份整个数据库(全备)

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

mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| doudou           |
| linfan           |
+------------------+
2 rows in set (0.00 sec)

[[email protected] ~]# ls
anaconda-ks.cfg  doudou.repo  lin  nfs.sh

[[email protected] ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-20180818.sql
Enter password:
[[email protected] ~]# ls
all-20180818.sql  anaconda-ks.cfg  doudou.repo  lin  nfs.sh

备份linfan库的doudou表


[[email protected] ~]# mysqldump -uroot -p -h127.0.0.1 linfan doudou > tabledoudou-20180818.sql
Enter password:
[[email protected] ~]# ls
all-20180818.sql  anaconda-ks.cfg  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql

备份linfan数据库

[[email protected] ~]# mysqldump -uroot -p -h127.0.0.1 --databases linfan > data-linfan-20180818.sql
Enter password:
[[email protected] ~]# ls
all-20180818.sql  anaconda-ks.cfg  data-linfan-20180818.sql  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql

模拟误删linfan数据库

mysql> drop  database linfan ;
Query OK, 2 rows affected (0.02 sec)

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

2.3 mysql数据恢复

[[email protected] ~]# ls
all-20180818.sql  anaconda-ks.cfg  data-linfan-20180818.sql  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql
[[email protected] ~]# mysql -uroot -p -h127.0.0.1 < all-20180818.sql
Enter password:
[[email protected] ~]# mysql -uroot -p -h127.0.0.1 -e ‘show databases;‘
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linfan             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

恢复linfan数据库的doudou表

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> source tabledoudou-20180818.sql;
mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| doudou           |
| linfan           |
+------------------+
2 rows in set (0.00 sec)

模拟删除整个数据库

mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| doudou           |
| linfan           |
+------------------+
2 rows in set (0.00 sec)

mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database‘ at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linfan             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

恢复整个数据库

[[email protected] ~]# ls
all-20180818.sql  anaconda-ks.cfg  data-linfan-20180818.sql  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql
[[email protected] ~]# mysql -uroot -p -h127.0.0.1 < all-20180818.sql
Enter password:
[[email protected] ~]# mysql -uroot -p -h127.0.0.1  -e ‘show databases;‘
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linfan             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

3.二进制格式mysql安装

下载二进制格式的mysql软件包

[[email protected] ~]# cd /usr/src/
[[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

解压软件至/usr/local/

[[email protected] src]# ls
apr-1.6.3          apr-util-1.6.1          debug    mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
apr-1.6.3.tar.bz2  apr-util-1.6.1.tar.bz2  kernels
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# ls  /usr/local/
apache  apr-util  etc    include  lib64    mysql-5.7.22-linux-glibc2.12-x86_64  share
apr     bin       games  lib      libexec  sbin                                 src
[[email protected] src]# cd  /usr/local/
[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[[email protected] local]# ll
total 0
drwxr-xr-x. 13 root root 152 Aug 17 12:46 apache
drwxr-xr-x.  6 root root  58 Aug 17 12:35 apr
drwxr-xr-x.  5 root root  43 Aug 17 12:40 apr-util
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx.  1 root root  36 Aug 17 13:54 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Aug 17 13:30 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root   6 Nov  5  2016 sbin
drwxr-xr-x.  5 root root  49 Jul 11 15:44 share
drwxr-xr-x.  2 root root   6 Nov  5  2016 src

修改目录/usr/locaal/mysql的属主属组

[[email protected] ~]# chown -R mysql.mysql /usr/local/mysql
[[email protected] ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Aug 17 13:54 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

添加环境变量

[[email protected] ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[[email protected] ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] ~]# . /etc/profile.d/mysql.sh
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

建立数据存放目录

[[email protected] ~]# cd /usr/local/mysql
[[email protected] mysql]# mkdir /opt/data
[[email protected] mysql]# chown -R mysql.mysql /opt/data/
[[email protected] mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql   6 Aug 17 14:05 data
drwxr-xr-x. 8 root  root  220 Jul 18 17:09 lin.d

初始化数据库

[[email protected] mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-17T06:08:33.347313Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-17T06:08:33.873415Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-17T06:08:33.953310Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-17T06:08:34.016549Z 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: f8e46285-a1e3-11e8-b6bf-000c29c9d4ed.
2018-08-17T06:08:34.019542Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2018-08-17T06:08:34.023380Z 1 [Note] A temporary password is generated for [email protected]: B<HiGFoc.8yZ
//这个命令的最后会生成一个临时密码,此处密码是B<HiGFoc.8yZ

生成配置文件

[[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] ~]# 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] ~]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/linfan.err‘.
 SUCCESS!
[[email protected] ~]# ps -ef|grep mysql
root      52200      1  0 14:25 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     52378  52200  4 14:25 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=linfan.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      52408   2998  0 14:25 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                              :::80                                           :::*
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.22

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(‘linfan123‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

原文地址:http://blog.51cto.com/13858192/2161424

时间: 2024-10-09 21:06:55

mysql进阶简单解析的相关文章

MySQL配置文件简单解析

1 [mysqld] 2 basedir = /data/mysql 3 datadir = /data/mysqldata 4 tmpdir = /data/mysqltmpdata //mysql的查询临时目录,避免因为磁盘空间不足,造成相关粗偶 5 port = 3306 6 socket = /data/mysql/mysql.sock 7 init-connect='SET NAMES utf8' //连接的字符集为utf8 8 character-set-server = utf8

Mysql Join语法解析与性能分析详解

一.Join语法概述 join 用于多表中字段之间的联系,语法如下: ... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona table1:左表:table2:右表. JOIN 按照功能大致分为如下三类: INNER JOIN(内连接,或等值连接):取得两个表中存在连接匹配关系的记录. LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(table2)并无对应匹配记录. RIGHT JOIN(右连接):与 LEF

mysql 存储过程简单学习

转载自:http://blog.chinaunix.net/uid-23302288-id-3785111.html ■存储过程Stored Procedure 存储过程就是保存一系列SQL命令的集合,将这些sql命令有组织的形成一个小程序,这样会实现很复杂的处理 SQL基本是一个命令一个命令执行,虽然可以通过连接.子查询等实现些高级的处理,但局限性是显而易见的 ■存储过程的优势 1.提高执行性能(存储过程事先完成了解析.编译的处理,执行时能减轻数据库负担) 2.可减轻网络负担(比起多次传递SQ

MySQL进阶5--分组排序和分组查询 group by(having) /order by

MySQL进阶--分组排序和分组查询 group by(having) /order by /* 介绍分组函数 功能:用做统计使用,又称为聚合函数或组函数 1.分类: sum, avg 求和 /平均数, 只处理数值型,都绝对忽略NULL值(avg处理时统计的个数没有null项) max ,min ,可以求字符串最大最小 ,可以匹配日期,都绝对忽略NULL值 count ,不计算NULL ,不把null算进数里 #2. 参数支持类型 SELECT MIN(last_name) ,MAX(last_

ngx lua模块源码简单解析

ngx lua模块源码简单解析分类: nginx 2014-07-11 11:45 2097人阅读 评论(0) 收藏 举报nginxlua数据结构架构目录(?)[+]对nginx lua模块的整个流程,原理简单解析.由于nginx lua模块相关配置,指令,API非常多,所以本文档只以content_by_lua指令举例说明. 读本文档最好配合读源码. 不适合对nginx和lua一点都不了解的人看.1.相关配置详细配置见 https://github.com/openresty/lua-ngin

Ubuntu 安装mysql和简单操作

ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client 3.  sudo apt-get install libmysqlclient-dev 安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后可以使用如下命令来检查是否安装成功: sudo netstat -tap | grep mysql 通过上述命令检查之后,如果看到有mysql 的

MySql进阶

[MySql进阶] 1.Order By 你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升排列. 实例: 2.Join语法 Join不是一个关键字 3.Left Join MySQL left join 与 join 有所不同. MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据. 以上实例中使用了LEFT JOIN,该语句会读取左边的数据表tutorials_tbl的所有选取的字段数据,即便在右侧表tcount_tb

MySQL Study之--MySQL体系结构深入解析

MySQL Study之--MySQL体系结构深入解析 MySQL体系架构 由连接池组件.管理服务和工具组件.sql接口组件.查询分析器组件.优化器组件.缓冲组件.插件式存储引擎.物理文件组成.mysql是独有的插件式体系结构,各个存储引擎有自己的特点. MySQL内存结构: Mysql 进程结构 Mysql不像oracle那样是通过多进程来完成其功能的.默认情况下,InnoDB存储引擎的后台线程有7个: 4个IO thread, 1个master thread, 1个锁(lock)监控线程,

【Android进阶】解析XML文件之使用DOM解析器

在前面的文章中,介绍了使用SAX解析器对XML文件进行解析,SAX解析器的优点就是占用内存小.这篇文章主要介绍使用DOM解析器对XML文件进行解析.DOM解析器的优点可能是理解起来比较的直观,当然,每个人对不同的解析方法可能有不同的喜好.但是DOM解析器有个比较大的缺点,就是占用内存比较多,在Android中的XML解析中,还是更推荐其他的解析方式. 下面介绍使用DOM解析器对XML进行解析. 下面是我们需要解析的xml文件 <?xml version="1.0" encodin