MySQL管理

用户相关

  • 创建用户

    -- 创建本地用户
    mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';
    -- 创建用户并授予ip为6.6.6.6的主机访问权限
    mysql> CREATE USER 'newuser'@'6.6.6.6' IDENTIFIED BY 'user_password';
    -- 创建可以从任何主机连接的用户,使用'%'通配符作为主机部分
    mysql> CREATE USER 'newuser'@'%' IDENTIFIED BY 'user_password';
    -- 创建本地用户之前检查是否已存在同名用户
    CREATE USER IF NOT EXISTS 'newuser'@'localhost' IDENTIFIED BY 'user_password';
  • 赋予用户权限

    • 常用权限:

      • ALL PRIVILEGES –授予用户帐户所有特权。
    • CREATE–允许用户帐户创建数据库和表。

    • DELETE -允许用户帐户从特定表中删除行。

      • INSERT -允许用户帐户在特定表中插入行。
    • SELECT –允许用户帐户读取数据库。
      • UPDATE -允许用户帐户更新表行。

      点击查看更多

    • 要向用户帐户授予特定特权,可以使用以下语法

    -- 授予特定数据库上的所有特权给某用户帐户
    mysql> GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';
    
    -- 授予所有数据库的所有特权给某用户帐户
    -- 可以省略 PRIVILEGES
    mysql> GRANT ALL  ON *.* TO 'database_user'@'localhost';
    
    -- 授予特定数据库上特定表的所有特权给某用户帐户
    mysql> GRANT ALL PRIVILEGES ON database_name.table_name TO 'database_user'@'localhost';
    
    -- -- 授予特定数据库上的多个特权给某用户帐户
    mysql> GRANT SELECT, INSERT, DELETE ON database_name.* TO 'localhost';
    • 显示某用户账户拥有的特权
    mysql> SHOW GRANTS FOR 'database_user'@'localhost';
    • 撤销某用户账户的特权
    -- 撤消特定数据库上用户帐户的所有特权
    mysql> REVOKE ALL PRIVILEGES ON database_name.* FROM 'database_user'@'localhost';
  • 删除现有的MySQL用户帐户

    mysql> DROP USER 'user'@'localhost'

数据库相关

查询数据/状态时如果嫌显示的格式不好看,可以用\G代替行尾分号

  • \G的作用和;是等效的
  • \G的作用是以垂直格式返回结果集(使每个字段打印到单独的行),如果返回的列数较大,则有时更易于阅读
MySQL [(none)]> help
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.
  • 创建/删除某数据库的方法

    把下面所有命令的DROP改为CREATE即为创建之法

    • 在数据库视图中
    -- 直接删除
    mysql> DROP DATABASE database_name;
    -- 若不确定该数据库是否存在
    mysql> DROP DATABASE IF EXISTS database_name;
    • 使用mysqladmin
    $ mysqladmin -u root -p drop database_name
  • 连接查询

    若仅有一个join,未指定连接类型,则默认内链接,

MySQL管理

  • 存储过程 (Stored Procedures)

  • 视图 (Views)

    -- 创建视图
    CREATE VIEW userInfo -- 视图名称
    AS  -- 下面跟正常查询语句即可
    SELECT
      u.enabled,lu.user_id,lu.domain_id,lu.name 
    FROM user u 
    JOIN
      local_user lu ON lu.user_id=u.id;
    
    -- 然后此视图即可视为一张表被查询
  • 触发器 (Triggers)

    -- 创建触发器结构
    CREATE TRIGGER trigger_name -- 触发器名必须在每个表中唯一
    {BEFORE | AFTER} {INSERT | UPDATE| DELETE } -- 指定触发器执行时间、以及激活触发器的动作
    ON table_name FOR EACH ROW -- 指定触发器所属的表的名称
    trigger_body; -- 指定触发触发器时要执行的语句
    
    -- 只有表才支持触发器,视图不支持(临时表也不 支持)
  • 事件 (Events)

  • 索引 (Indexes)

相关推荐