Mysql基础管理

一、Mysql基础管理

1.用户管理

用户的作用

linux用户:登录linux系统; 管理linux对象-文件

Mysql用户: 登录mysql数据库 管理mysql对象-表

用户的定义

linux用户: 用户名

mysql用户: 用户名@‘白名单’

白名单:地址列表,允许白名单的ip登录mysql,管理mysql

# 白名单
cdan@‘localhost’:cdan用户能够通过本地登录mysql(socket)
cdan@‘10.0.0.10’,cdan用户能够通过10.0.0.10远程登录mysql服务器
cdan@‘10.0.0.%’,cdan用户能够通过10.0.0.xx/24远程登录mysql服务器
cdan@‘10.0.0.%5’,...50-59...
cdan@‘%’,白名单失效

查用户

select user,host,authentication_string from mysql.user;

增用户

create user longlong@'localhost';
create user cdan@'192.168.15.%' identified by 'cdan@111';

改用户

alter user longlong@'localhost' identified by 'cdan@111';

删用户

drop user longlong@'localhost';

2.权限管理

作用:用户对数据库对象,有哪些管理能力

权限的表现方式:具体的命令

mysql> show privileges;

授权、回收权限操作

# 语法
8.0以前
grant 权限 on 对象 to 用户 identified by '密码';

8.0以后
create user 用户 identified by '密码';
grant 权限 on 对象 to 用户;

# 权限
all:管理员
权限1,权限2,权限3:普通用户(业务用户,开发用户)
grant option: 给别的用户授权

# 对象范围:库、表
*.*  对应 chmod -R 755 /            管理员
cdan.* 对应 chmod -R 755 /cdan      普通用户
cdan.t1 对应 chmod -R 755 /cdan/t1

授权的例子

1.创建并授权管理员用户,能够通过192.168.15.%网段登录并管理数据库
mysql> grant all on *.* to dan@'192.168.15.%' identified by 'Cdan@111' with grant option;

查询创建用户
mysql> select user,host from mysql.user;

查询用户权限
mysql> show grants for cdan@'192.168.15.%';
mysql> select * from mysql.user\G
2.创建并授权一个app@’192.168.15.%‘业务用户,能够对app库下所有对象进行create,select,update,delete,insert操作
mysql> grant create,select,update,delete,insert on app.* to app@'192.168.15.%' identified by 'Cdan@111';

mysql> show grants for app@'192.168.15.%';
+---------------------------------------------------------------------------------+
| Grants for app@192.168.15.%                                                     |
+---------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'app'@'192.168.15.%'                                      |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ON `app`.* TO 'app'@'192.168.15.%' |
+---------------------------------------------------------------------------------+

回收权限

mysql> revoke create on app.* from 'app'@'192.168.15.%';

超级管理员忘记密码

--skip-grant-tables :跳过授权表
--skip-networking :跳过TCP/IP连接

3.连接管理

1)mysql自带客户端

# mysql
参数列表:
 -u   用户名
 -p   密码
 -S   本地socket文件位置
 -h   数据库ip地址
 -P   数据库端口号
 -e   面交互执行数据库命令
  <   导入sql脚本
  
socket前提:数据库中必须实现授权cdan@‘192.168.15.%’用户
mysql -ucdan -pCdan@111 -S /tmp/mysql.sock
mysql -ucdan -p -S /tmp/mysql.sock
mysql -p123 -S /tmp/mysql.sock

TCP/IP前提:必须提前创建好,可以远程连接的用户(例如:cdan@‘192.168.15.%’)
mysql -ucdan -p123 -h 192.168.15.10 -P 3306
mysql -ucdan -p123 -h 192.168.15.10 
mysql -ucdan -p -h 192.168.15.10 -P 3306
例子
授权lili管理用户,可以通过本地socket登录
1.创建lili用户
mysql> grant all on *.* to lili@'localhost' identified by 'Cdan@111';
2.登录测试
mysql -ulili -p'Cdan@111' -S /tmp/mysql.sock

2)mysql远程客户端程序

3)程序连接

原文地址:https://www.cnblogs.com/caodan01/p/15755989.html