• 第四章 数据库的权限管理


    一、用户权限管理

    1.授权的命令

    grant all on *.* to root@'172.16.1.%' identified by '123';
    
    grant all on *.* to root@'172.16.1.%' identified by '123' with grant option;
    

    2.作用对象

    grant all on *.* to root@'172.16.1.%' identified by '123';          #所有库下的所有表
    grant all on mysql.* to root@'172.16.1.%' identified by '123';		#mysql库下的所有表,单库授权
    grant all on mysql.user to root@'172.16.1.%' identified by '123';	#单表授权
    
    #单列授权,企业称之为脱敏,脱敏:脱离敏感信息
    1.普通用户只能查看用户名字
    	mysql> grant select(user) on mysql.user to diaosi@'172.16.1.%' identified by '123';
    2.VIP用户可以查看用户用户较多的信息
    	mysql> grant select(user,host) on mysql.user to diaosi@'172.16.1.%' identified by '123';
    3.超级VIP可以查看用户所有的信息
    	mysql> grant select on mysql.user to diaosi@'172.16.1.%' identified by '123';
    

    3.在企业里一般我们怎么给权限

    #开发要一个用户登录数据库
    1.跟开发沟通:
    	1)你要操作的是哪个库哪个表
    	2)你要对这个表进行哪些操作
    	3)你从哪个IP连过来
    	4)你对用户名的要求
    	5)你对密码有什么要求
    	6)你的用户要用多久
    	7)发邮件
    2.一般怎么授权
    	grant select,insert,update on mysql.* to dev@'172.16.1.51' identified by 'Dev@123456';
    3.开发要root用户怎么办?
    

    4.权限实践

    1)准备数据库

    #创建wordpress数据库
    create database wordpress;
    #使用wordpress库
    use wordpress;
    #创建t1、t2表
    create table t1 (id int);
    create table t2 (id int);
    
    #创建blog库
    create database blog;
    #使用blog库
    use blog;
    #创建t1表
    create table tb1 (id int);
    

    2)授权

    #授权wordpress@'10.0.0.5%'这个用户对所有库所有表有查询权限,密码是123
    #授权wordpress可以通过10.0.0.5%这个网段连接数据库对所有库所有表有查询权限,密码是123
    1.grant select on *.* to wordpress@'10.0.0.5%' identified by '123';
    
    #授权wordpress@'10.0.0.5%'对wordpress库下的所有表有增删改的权限,密码是123
    #授权wordpress可以通过10.0.0.5%这个网段连接数据库对wordpress库下的所有表有增删改的权限,密码是123
    2.grant insert,delete,update on wordpress.* to wordpress@'10.0.0.5%' identified by '123';
    
    #授权wordpress@'10.0.0.5%'对wordpress下面的t1表有所有权限,密码是123
    3.grant all on wordpress.t1 to wordpress@'10.0.0.5%' identified by '123';
    

    3)请问

    一个客户端程序使用wordpress用户登陆到10.0.0.51的MySQL后,
    1.对t1表的管理能力?			#所有权限
    2.对t2表的管理能力?			#增删改查
    3.对tb1表的管理能力?			#只有查
    

    4)授权总结

    1.不同级别授权,权限是相加关系
    2.不推荐在多级别定义重复的权限
    3.常见的授权方式是单库授权
    	grant select,insert,update on mysql.* to dev@'172.16.1.51' identified by 'Dev@123456';
    4.如果涉及到用户的敏感信息,可以使用脱敏,单列授权
    5.查看用户的权限
    	mysql> show grants for root@'172.16.1.%';
    

    二、MySQL连接管理

    1.连接工具

    1)MySQL自带的客户端命令

    #自带的命令 mysql
    -u:		指定用户		mysql -uroot
    -p:		指定密码		mysql -uroot -p123
    -h:		指定主机		mysql -uroot -p123 -h172.16.1.51
    -P:		指定端口		mysql -uroot -p123 -P3307 -h172.16.1.51
    -S:		指定sock		  mysql -uroot -p123 -S /tmp/mysql.sock
    -e:		指定SQL		  mysql -uroot -p123 -e "show databases"
    #--protocol=name:指定连接方式
    

    2)第三方客户端工具

    1.Navicat
    2.SQLyog
    

    2.连接方式

    1)socket连接

    mysql -uroot -p123
    mysql -uroot -p123 -S /tmp/mysql.sock
    

    2)TCP/IP连接

    mysql -uroot -p123 -h172.16.1.51
    

    三、MySQL启动关闭流程

    1.启动数据库

    1./etc/init.d/mysqld start  --->  mysqld_safe  --->  mysqld 
    
    2.systemctl start mysqld  --->  mysqld_safe  --->  mysqld
    
    3.mysqld_safe --defaults-file=/etc/my.cnf &  --->  mysqld_safe  --->  mysqld
    

    2.关闭数据库

    #推荐的方式:
    1./etc/init.d/mysqld stop
    2.systemctl stop mysqld
    3.mysqladmin shutdown
    4.service mysql stop
    
    #不推荐的方式:
    1.kill -9 mysqlpid
    2.pkill mysqld
    3.killall mysqld
    #错误关闭会导致:
    1.数据丢失
    2.数据库启动故障
    

    四、MySQL配置管理

    1.配置mysql的方法

    1.编译的时候
    #程序存放位置
    cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.38 
    #数据存放位置
    -DMYSQL_DATADIR=/application/mysql-5.6.38/data 
    #socket文件存放位置
    -DMYSQL_UNIX_ADDR=/application/mysql-5.6.38/tmp/mysql.sock 
    ... ...
    
    2.初始化
    /mysql_install_db --defaults-file=/service/3309/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3309/data
    
    --datadir=/application/mysql/data
    --basedir=/application/mysql
    --defaults-file=/etc/my.cnf
    --pid-file=/application/mysql/data/db01.pid
    --socket=/application/mysql/data/mysql.sock
    --user=mysql
    --port=3306
    --log-error=/application/mysql/data/db01.err
    
    3.配置文件
    /etc/my.cnf
    /etc/mysql/my.cnf
    $basedir/my.cnf
    ~/my.cnf
    
    #defaults-extra-file (类似include)
    

    2.数据库配置文件

    1)配置文件都有哪些

    /etc/my.cnf
    /etc/mysql/my.cnf
    $basedir/my.cnf
    ~/.my.cnf
    

    2)配置文件的读取顺序

    /etc/my.cnf
    /etc/mysql/my.cnf
    $basedir/my.cnf
    ~/.my.cnf
    

    3)配置文件生效顺序

    ~/.my.cnf
    $basedir/my.cnf
    /etc/mysql/my.cnf
    /etc/my.cnf
    

    4)测试配置文件生效顺序

    #1.配置/etc/my.cnf
    [root@db02 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=1
    
    #2.配置/etc/mysql/my.cnf
    [root@db02 ~]# vim /etc/mysql/my.cnf
    [mysqld]
    server_id=2
    
    #3.配置$basedir/my.cnf
    [root@db02 ~]# vim /service/mysql/my.cnf 
    [mysqld]
    server_id=3
    
    #4.配置~/my.cnf
    [root@db02 ~]# vim ~/.my.cnf
    [mysqld]
    server_id=4
    
    #5.重启数据库
    [root@db02 ~]# systemctl stop mysqld
    [root@db02 ~]# /etc/init.d/mysqld start
    Starting MySQL. SUCCESS!
    
    #6.查看server_id
    [root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
    Warning: Using a password on the command line interface can be insecure.
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 4     |
    +---------------+-------+
    
    #7.删除生效的配置重启,再次查看id
    [root@db02 ~]# rm -rf ~/.my.cnf
    [root@db02 ~]# /etc/init.d/mysqld restart
    [root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
    Warning: Using a password on the command line interface can be insecure.
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 3     |
    +---------------+-------+
    
    #8.删除生效的配置重启,再次查看id
    [root@db02 ~]# rm -rf /service/mysql/my.cnf
    [root@db02 ~]# /etc/init.d/mysqld restart
    [root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
    Warning: Using a password on the command line interface can be insecure.
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 2     |
    +---------------+-------+
    
    #9.删除生效的配置重启,再次查看id
    [root@db02 ~]# rm -rf /etc/mysql/my.cnf
    [root@db02 ~]# /etc/init.d/mysqld restart
    Shutting down MySQL.. SUCCESS! 
    Starting MySQL.. SUCCESS! 
    [root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
    Warning: Using a password on the command line interface can be insecure.
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 1     |
    +---------------+-------+
    

    3.执行参数优先级

    1)socket文件指定位置

    #cmake:
    socket=/application/mysql/tmp/mysql.sock
    
    #命令行:
    --socket=/tmp/mysql.sock
    
    #配置文件:
    /etc/my.cnf 中[mysqld]标签下:socket=/opt/mysql.sock
    
    #default参数:
    --defaults-file=/tmp/a.txt 配置文件中[mysqld]标签下:socket=/tmp/test.sock
    

    2)测试优先级

    #1.启动MySQL,查看sock文件位置
    [root@db02 ~]# mysqld_safe --defaults-file=/tmp/a.txt --socket=/tmp/mysql.sock &
    [root@db02 ~]# ll /tmp/
    srwxrwxrwx 1 mysql mysql         0 Oct 22 18:58 mysql.sock
    
    #2.启动MySQL,查看sock文件位置
    [root@db02 ~]# mysqld_safe --defaults-file=/tmp/a.txt &
    [root@db02 ~]# ll /tmp/
    srwxrwxrwx 1 mysql mysql         0 Oct 22 19:01 test.sock
    #如果设置--defaults-file,那么MySQL启动时只读取指定的配置文件
    
    #3.启动MySQL,查看sock文件位置
    [root@db02 ~]# mysqld_safe &
    [root@db02 ~]# ll /opt/
    srwxrwxrwx 1 mysql mysql 0 Oct 22 19:03 mysql.sock
    

    4.优先级总结

    #优先级排序
    命令行  >  --defaults-file参数指定配置  >  ~/.my.cnf  >  $basedir/my.cnf  >  /etc/mysql/my.cnf  >  /etc/my.cnf  >  初始化配置   >  cmake
    

    5.配置文件作用

    1)作用

    1.影响客户端的连接
    2.影响服务端的启动
    

    2)影响客户端的连接

    [root@db02 ~]# vim /etc/my.cnf
    #配置文件添加连接数据库信息的配置
    [mysql]
    user=root
    password=123456
    

    3)影响服务端的启动

    #修改server_id
    [root@db02 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=2
    
    #查看配置没有生效
    [root@db02 ~]# mysql -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 1     |
    +---------------+-------+
    
    #重启数据库后,修改的服务端配置才会生效
    [root@db02 ~]# systemctl restart mysqld 
    [root@db02 ~]# mysql -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 2     |
    +---------------+-------+
    

    4)总结

    1.客户端配置受配置文件影响
    2.客户端配置修改后不需要重启服务			[mysql] 或者 [client]
    3.服务端配置修改后需要重启服务后生效		   [mysqld] 或者 [server]
    
    4.常见配置
    [root@db02 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=2
    socket=/tmp/mysql.sock
    
    [mysql]
    socket=/tmp/mysql.sock
    

    五、MySQL的命令

    1.mysql内部快捷命令

    c:		终止正在输入的命令
    	
    :		重新连接数据库
    	d:		修改sql语句结束符
    	e:		输出输入的内容
    G:		以key:values形式展示数据
    q:		退出数据库
    	g:		结束语句
    	h: 	查看帮助
    	T:		输出一个文件记录操作
    		:		终止记录操作的文件
    	p:		打印输入的命令
    	R:		 临时修改命令提示符
    .		导入sql语句,等于 source 命令
    	s:		查看数据库连接状态
    !:		在数据库里执行命令行命令
    u		切换数据库,等于 use 命令
    

    2.help命令

    #查看数据库内命令语法
    mysql> help
    mysql> help create database
    mysql> help create table
    mysql> help select
    

    2.客户端mysqladmin命令

    1)设置密码

    #1.设置密码
    [root@db02 ~]# mysqladmin -uroot password
    New password: 
    Confirm new password:
    
    #2.修改密码
    [root@db02 ~]# mysqladmin -uroot -p password
    Enter password: 
    New password: 
    Confirm new password:
    

    2)关闭服务

    [root@db02 ~]# mysqladmin -uroot -p shutdown 
    Enter password: 123
    

    3)库外建库

    [root@db02 ~]# mysqladmin -uroot -p123 create database222222
    

    4)库外删库

    [root@db02 ~]# mysqladmin -uroot -p123 drop database222222
    Warning: Using a password on the command line interface can be insecure.
    Dropping the database is potentially a very bad thing to do.
    Any data stored in the database will be destroyed.
    
    Do you really want to drop the 'database222222' database [y/N] y
    Database "database222222" dropped
    

    5)查看数据库配置

    [root@db02 ~]# mysqladmin -uroot -p123 variables | grep -w server_id
    

    6)检测数据库是否启动

    [root@db02 ~]# mysqladmin -uroot -p ping
    Enter password: 
    mysqld is alive
    

    7)查看数据库信息

    [root@db02 ~]# mysqladmin -uroot -p123 status
    Warning: Using a password on the command line interface can be insecure.
    Uptime: 130  Threads: 1  Questions: 94  Slow queries: 0  Opens: 70  Flush tables: 1  Open tables: 63  Queries per second avg: 0.723
    
    Slow queries: 0
    Queries per second avg: 0.723
    

    8)刷新授权表

    [root@db02 ~]# mysqladmin -uroot -p123 reload
    

    9)刷新binlog

    [root@db02 ~]# mysql -uroot -p123 -e "show master status"
    Warning: Using a password on the command line interface can be insecure.
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 |      120 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    
    #刷新binlog
    [root@db02 ~]# mysqladmin -uroot -p123 flush-logs 
    Warning: Using a password on the command line interface can be insecure.
    
    #再次查看binlog
    [root@db02 ~]# mysql -uroot -p123 -e "show master status"
    Warning: Using a password on the command line interface can be insecure.
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000002 |      120 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    
  • 相关阅读:
    ORACLE 查询所有用户调度作业
    【ORACLE】查看死锁进程并结束死锁的脚本
    Oracle12c中数据泵新特性之功能增强(expdp, impdp)
    oracle常用查询sql
    记一次异机rman还原后的操作
    Oracle 11gR2 RAC网络配置,更改public ip、vip和scanip
    记一次异机rman还原后的操作
    oracle通过impdp导入不同表用户、不同表空间的数据
    telegram即时通信软件和outline ---- by 余弦 by倾旋
    Vue -3:单文件组件
  • 原文地址:https://www.cnblogs.com/jhno1/p/13879380.html
Copyright © 2020-2023  润新知