GRANT ALL ON *.* TO anyname@localhost IDENTIFIED BY "passwd" WITH GRANT OPTION 该语句将在user表中为anyname@localhost创建一个记录,打开所有权限,因为这里是超级用户(全局)权限存储的地方,要用INSERT语句做同样的事情,语句是:
INSERT INTO user VALUES("localhost","anyname",PASSWORD("passwd"), "Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y") 你可能发现它不工作,这要看你的MySQL版本。授权表的结构已经改变而且你在你的user表可能没有14个权限列。用SHOW COLUMNS找出你的授权表包含的每个权限列,相应地调整你的INSERT语句。 下列GRANT语句也创建一个拥有超级用户身份的用户,但是只有一个单个的权限:
GRANT RELOAD ON *.* TO flush@localhost IDENTIFIED BY "flushpass" 本例的INSERT语句比前一个简单,它很容易列出列名并只指定一个权限列。所有其它列将设置为缺省的"N":
INSERT INTO user (Host,Password,Reload) VALUES("localhost","flush",PASSWORD("flushpass"),"Y") 数据库级权限用一个ON db_name.*子句而不是ON *.*进行授权:
GRANT ALL ON sample.* TO boris@localhost IDENTIFIED BY "ruby" 这些权限不是全局的,所以它们不存储在user表中,我们仍然需要在user表中创建一条记录(使得用户能连接),但我们也需要创建一个db表记录记录数据库集权限:
INSERT INTO user (Host,User,Password) VALUES("localhost","boris",PASSWORD("ruby"))
INSERT INTO db VALUES("localhost","sample_db","boris","Y","Y","Y","Y","Y","Y","N","Y","Y","Y")
"N"列是为GRANT权限;对末尾的一个数据库级具有WITH GRANT OPTION的GRANT语句,你要设置该列为"Y"。