Mysql创建数据库、创建用户、给用户赋权限

数据库 2096 0 2012-08-05

Mysql创建数据库、创建用户、给用户赋权限

使用mysqladmin进行数据库的创建。
登录时要带上用户名。如:mysqladmin -u root create db_test
输入用户密码后,数据库即可创建成功。

mysqladmin
mysqladmin  Ver 8.42 Distrib 5.1.19-beta, for portbld-freebsd6.0 on i386
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Administration program for the mysqld daemon.
Usage: mysqladmin [OPTIONS] command command....
  -c, --count=#       Number of iterations to make. This works with -i
                      (--sleep) only.
  -#, --debug[=name]  Output debug log. Often this is 'd:t:o,filename'.
  --debug-info        Print some debug info at exit.
  -f, --force         Don't ask for confirmation on drop database; with
                      multiple commands, continue even if an error occurs.
  -C, --compress      Use compression in server/client protocol.
  --character-sets-dir=name
                      Directory where character sets are.
  --default-character-set=name
                      Set the default character set.
  -?, --help          Display this help and exit.
  -h, --host=name     Connect to host.
  -b, --no-beep       Turn off beep on error.
  -p, --password[=name]
                      Password to use when connecting to server. If password is
                      not given it's asked from the tty.
  -P, --port=#        Port number to use for connection.
  --protocol=name     The protocol of connection (tcp,socket,pipe,memory).
  -r, --relative      Show difference between current and previous values when
                      used with -i. Currently works only with extended-status.
  -O, --set-variable=name
                      Change the value of a variable. Please note that this
                      option is deprecated; you can set variables directly with
                      --variable-name=value.
  -s, --silent        Silently exit if one can't connect to server.
  -S, --socket=name   Socket file to use for connection.
  -i, --sleep=#       Execute commands again and again with a sleep between.
  -u, --user=name     User for login if not current user.
  -v, --verbose       Write more information.
  -V, --version       Output version information and exit.
  -E, --vertical      Print output vertically. Is similar to --relative, but
                      prints output vertically.
  -w, --wait[=#]      Wait and retry if connection is down.
  --connect_timeout=#
  --shutdown_timeout=#

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- -----------------------------
count                             0
debug-info                        FALSE
force                             FALSE
compress                          FALSE
character-sets-dir                (No default value)
default-character-set             (No default value)
host                              (No default value)
no-beep                           FALSE
port                              0
relative                          FALSE
socket                            (No default value)
sleep                             0
user                              (No default value)
verbose                           FALSE
vertical                          FALSE
connect_timeout                   43200
shutdown_timeout                  3600

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf /usr/local/etc/mysql/my.cnf ~/.my.cnf /usr/local/etc/my.cnf
The following groups are read: mysqladmin client
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit
--no-defaults           Don't read default options from any options file
--defaults-file=#       Only read default options from the given file #
--defaults-extra-file=# Read this file after the global files are read

Where command is a one or more of: (Commands may be shortened)
  create databasename   Create a new database
  debug                 Instruct server to write debug information to log
  drop databasename     Delete a database and all its tables
  extended-status       Gives an extended status message from the server
  flush-hosts           Flush all cached hosts
  flush-logs            Flush all logs
  flush-status          Clear status variables
  flush-tables          Flush all tables
  flush-threads         Flush the thread cache
  flush-privileges      Reload grant tables (same as reload)
  kill id,id,...        Kill mysql threads
  password new-password Change old password to new-password, MySQL 4.1 hashing.
  old-password new-password Change old password to new-password in old format.

  ping                  Check if mysqld is alive
  processlist           Show list of active threads in server
  reload                Reload grant tables
  refresh               Flush all tables and close and open logfiles
  shutdown              Take server down
  status                Gives a short status message from the server
  start-slave           Start slave
  stop-slave            Stop slave
  variables             Prints variables available
  version               Get version info from server

 

mysql中创建用户及授权2007-06-17 17:48GRANT 语句的语法如下:
     GRANT privileges (columns)
           ON what
           TO user IDENTIFIEDBY "password"
           WITH GRANT OPTION

对用户授权
mysql>grant rights on database.* to user@host identified by "pass";

例1:
   增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。
    grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";

      ON 子句中*.* 说明符的意思是“所有数据库,所有的表”

例2:
   增加一个用户test2密码为abc, 让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";

例子3
增加一个用户custom,他能从主机localhost、server.domain和whitehouse.gov连接。他只想 要从 localhost存取bankaccount数据库,从whitehouse.gov存取expenses数据库和从所有3台主机存取customer 数据库。他想要从所有3台主机上使用口令stupid。

为了使用GRANT语句设置个用户的权限,运行这些命令:

shell> mysql --user=root mysql

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
                ON bankaccount.* TO custom@localhost   IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
                ON expenses.*   TO custom@whitehouse.gov IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
                ON customer.* TO custom@'%'   IDENTIFIED BY 'stupid';

==============================================
权限信息用user、db、host、tables_priv和columns_priv表被存储在mysql数据库中(即在名为mysql的数据库中)。

权限            列                      Context
select           Select_priv          表                                     
insert           Insert_priv          表 
update         Update_priv      
delete           Delete_priv        
index            Index_priv        
alter             Alter_priv           
create          Create_priv          数据库、表或索引
drop             Drop_priv            数据库或表
grant            Grant_priv           数据库或表
references    References_priv    数据库或表
reload           Reload_priv          服务器管理
shutdown     Shutdown_priv        服务器管理
process          Process_priv           服务器管理
file                  File_priv                 在服务器上的文件存取

1.
select、insert、update和delete权限       
允许你在一个数据库现有的表上实施操作,是基本权限

2.
alter权限允许你使用ALTER TABLE

3.
create和drop权限允许你创建新的数据库和表,或抛弃(删除)现存的数据库和表
       如果你将mysql数据库的drop权限授予一个用户,该用户能抛弃存储了MySQL存取权限的数据库!

4.
grant权限允许你把你自己拥有的那些权限授给其他的用户。

你不能明显地指定一个给定用户应该被拒绝存取。即,你不能明显地匹配一个用户并且然后拒绝连接。
你不能指定一个用户有权创建立或抛弃一个数据库中的表,也不能创建或抛弃数据库本身。

可以同时列出许多被授予的单个权限。

例如,如果想让用户能读取和修改已有表的内容,但又不允许创建新表或删除表,可按如下授权:
     GRANT SELECT,INSERT,DELETE,UPDATE   ON samp_db.*    TO user@%
         IDENTIFIEDBY "pass"

上一篇:MYSQL安装出现could not start the service mysql error:0处理

下一篇:快捷版(Oracle 数据库 XE)下载

讨论数量:0

请先登录再发表讨论。 2024-05-01

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链