[root@1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456'; #建立user1用戶並授予其全部權限「*.*」(通配符) #第一個*表示db_name;第二個*表示tb_name #同時指定其來源IP127.0.0.1(即,只可經過此IP登陸) #此處能夠使用通配符%,表明全部IP(通常不使用) #設定密碼:identified by mysql> quit Bye
[root@1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. mysql> grant all on *.* to 'user2'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.01 sec) mysql> quit Bye
[root@1 ~]# mysql -uuser1 -p123456 -h127.0.0.1 Welcome to the MySQL monitor. mysql> quit Bye
[root@1 ~]# mysql -uuser2 -p'123456' Welcome to the MySQL monitor. mysql> exit Bye
說明: 由於指定登陸主機爲localhost,因此該用戶默認使用(監聽)本地mysql.socket文件,不須要指定IP便可登陸。mysql
[root@1 ~]# mysql -uroot -p'123456' Welcome to the MySQL monitor. mysql> create database db1; Query OK, 1 row affected (0.04 sec) mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.8.132' identified by '123456'; #建立user2用戶,並授予其針對db1庫SELECT,UPDATE,INSERT權限 mysql> grant all on db1.* to 'user'@'%' identified by '123456'; #建立user3,並針對全部IP授予其db1庫全部權限
[root@1 ~]# mysql -uroot -p'123456' Welcome to the MySQL monitor. mysql> show grants; #查看當前用戶的權限 mysql> show grants for user2@192.168.8.132; #查看指定用戶的權限
[root@1 ~]# mysql -uroot -p'123456' Welcome to the MySQL monitor. mysql> GRANT USAGE ON *.* TO 'user2'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB743291105EE4568DDA7DC67ED2CA2AD9'; Query OK, 0 rows affected (0.03 sec) mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'127.0.0.1'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for user2@127.0.0.1; +--------------------------------------------------------------------------------------------------------------+ | Grants for user2@127.0.0.1 | +--------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'user2'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'127.0.0.1' | +--------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> quit Bye
注: 更改用戶權限時,權限行的內容都要更改。sql
[root@1 ~]# mysql -uroot -p'123456'; Welcome to the MySQL monitor. mysql> use db1; Database changed #選擇庫 mysql> select count(*) from mysql.user; +----------+ | count(*) | +----------+ | 12 | +----------+ 1 row in set (0.04 sec) #查看指定庫的內容的行數 mysql> select * from mysql.db\G; #查看庫的全部內容 mysql> select db,user from mysql.db; #查看庫指定內容 mysql> select * from mysql.db where host like '192.168.%'\G; #查看某些IP對應的庫內容,like表示匹配 mysql> create table t1(`id` int(4),`name` char(40)); Query OK, 0 rows affected (0.39 sec) #在db1庫下建立表t1 mysql> select * from db1.t1; Empty set (0.03 sec) #查看錶中信息:空表 mysql> insert into db1.t1 values(1,'abc'); Query OK, 1 row affected (0.09 sec) #向表中插入內容 mysql> select * from db1.t1; +------+------+ | id | name | +------+------+ | 1 | abc | +------+------+ 1 row in set (0.00 sec) mysql> update db1.t1 set name='aaa' where id=1; Query OK, 1 row affected (0.08 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from db1.t1; +------+------+ | id | name | +------+------+ | 1 | aaa | +------+------+ 1 row in set (0.00 sec) #更改表中指定內容 mysql> delete from db1.t1 where id=1; Query OK, 2 rows affected (0.10 sec) #刪除表中指定內容 mysql> select * from db1.t1; Empty set (0.00 sec) mysql> truncate db1.t1; Query OK, 0 rows affected (0.09 sec) #清空一個表中內容 mysql> drop table t1; Query OK, 0 rows affected (0.04 sec) #刪除表 mysql> drop database db1; Query OK, 0 rows affected (0.13 sec) #刪除庫 mysql> use mysql; mysql> delete from user where User='user1' and Host='127.0.0.1'; Query OK, 1 row affected (0.06 sec) #刪除用戶,在刪除用戶前須要先指定表
備份指定庫: [root@1 ~]# mysqldump -uroot -p123456 mysql > /tmp/mysqlbak.sql 備份全部庫: [root@1 ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql
[root@1 ~]# mysql -uroot -p123456 < /tmp/mysqlbak.sql
備份指定表: [root@1 ~]# mysql -uroot -p123456 mysql user > /tmp/user.sql 只備份表結構: [root@1 ~]# mysqldump -uroot -p123456 -d mysql > /tmp/mysql_tb.sql
[root@1 ~]# mysql -uroot -p123456 mysql user < /tmp/user.sql