设为首页 加入收藏

TOP

Mysql常用基本命令汇总及默认账户权限与修改(二)
2015-07-24 10:43:17 来源: 作者: 【 】 浏览:3
Tags:Mysql 常用 基本 命令 汇总 默认 账户 权限 修改
e_Third_Wave | 100 | +----------------+------+ 1 row in set (0.00 sec)

更新表中数据

mysql> update table_name set age=188 where name="The_Third_Wave";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from table_name;
+----------------+------+
| name           | age  |
+----------------+------+
| The_Third_Wave |  188 |
+----------------+------+
1 row in set (0.00 sec)

mysql> 

导入数据到表

【待补充】

清空表(delete)

mysql> delete from table_name;
Query OK, 1 row affected (0.00 sec)

mysql> select * from table_name;
Empty set (0.00 sec)

删除数据库或表(drop)

mysql> drop table table_name;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> drop database mydata;
Query OK, 0 rows affected (0.00 sec)

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

权限管理

以下是安装后默认用户组:

mysql> use mysql;
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> select Host,User,Password from user;
+-----------------------------+------------------+-------------------------------------------+
| Host                        | User             | Password                                  |
+-----------------------------+------------------+-------------------------------------------+
| localhost                   | root             | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| xxxxxxxxxxxxxxxxxxxxxxxxxxx | root             |                                           |
| 127.0.0.1                   | root             |                                           |
| ::1                         | root             |                                           |
| localhost                   | debian-sys-maint | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+-----------------------------+------------------+-------------------------------------------+
5 rows in set (0.00 sec)

这里面中间三个host对应的root账户密码为空。【备注:不同版本的Mysql不一样,有些都会同时设置密码,不会像上面一样。】

看来我们前面只设置了Host:localhost对应的root账号的密码,我们使用mysql> select current_user();查看下当前登录的用户名:

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
注意user_name@host_name 这个user_name是用户名称, host_name是你选择连接到服务器的客户机的地址。而你在使用mysql -h host_name 这个host_name说的是服务器地址(上面有说明,注意区别)。

也就是说:比如你有一个msyql数据库在 db.csdn.com , 那你就mysql -u root -h db.csdn.com ,这和你自己的机器一点关系都没有,你的登录账号还是 root@yourPCname

这个可能不好理解,举个例子并练习下创建用户操作。

创建新用户并指定某个IP可以建立连接

实际我的操作是在我的试验机上数据中创建远程访问用户账号(原因是:本机安全第一,我可不想单独还去设置下防火墙,而我的试验机没有防火墙!),目的是提供远程主机(我所用的电脑)访问本机(我的试验机)数据库的权限!【这句话有点绕】,因为默认账号是不可以非本机登录访问的,我用默认root账号连接到我的试验机会报以下错误:

mysql -u root -h 我的试验机IP地址 -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'这里是我的本机IP地址' (using password: YES)

这样可以理解上面那段话吧?

接下来学习下用户权限设置,以下内容参考的是:MYSQL数据库管理之权限管理----飞鸿无痕

命令详解如下:

GRANT命令使用说明

先来看一个例子,创建一个只允许从本地登录的超级用户feihong,并允许将权限赋予别的用户,密码为test@feihong.111

GRANT ALL PRIVILEGES ON *.* TO feihong@'localhost' IDENTIFIED BY'test@feihong.111' WITH GRANT OPTION;

GRANT命令说明:

ALL PRIVILEGES 是表示所有权限,你也可以使用select、update等权限提到的权限。

ON 用来指定权限针对哪些库和表。

*.* 中前面的*号用来指定数据库名,后面的*号用来指定表名。

TO 表示将权限赋予某个用户。

feihong@'localhost' 表示feihong用户,@后面接限制的主机,可以是IP、IP段、域名以及%,%表示任何地方。注意:这里%有的版本不包括本地,以前碰到过给某个用户设置了%允许任何地方登录,但是在本地登录不了,这个和版本有关系,遇到这个问题再加一个localhost的用户就可以了。

IDENTIFIED BY 指定用户的登录密码。

WITH GRANT OP

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用xtrabackup实现MySQL主从复制 下一篇MySQL远程访问的两个问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·HyperText Transfer (2025-12-26 07:20:48)
·半小时搞懂 HTTP、HT (2025-12-26 07:20:42)
·CPython是什么?PyPy (2025-12-26 06:50:09)
·Python|如何安装seab (2025-12-26 06:50:06)
·python要学习数据分 (2025-12-26 06:50:03)