MySQL常規管理

本文包括如下內容:

 

匿名訪問test:
修改root密碼:
創建一個新用戶:
限制用戶權限到表和列:
一個用戶將自己所擁有的權限賦給另一個用戶:
取消用戶權限:
刪除一個用戶:
使用mysqldump備份一個數據庫:
使用mysqldump備份一個或多個表:
從備份文件恢復一個數據庫:

 

 

 

匿名訪問test:

C:/>mysql -h localhost test
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 6 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tab1           |
+----------------+
1 row in set (0.00 sec)


修改root密碼:

C:/>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 7 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> update user set password=password('0000') where user=root;
ERROR 1046: No Database Selected
mysql> create database mydata;
Query OK, 1 row affected (0.00 sec)

mysql> use mydata;
Database changed
mysql> update user set password=password('0000') where user=root;
ERROR 1146: Table 'mydata.user' doesn't exist
mysql> use mysql;
Database changed
mysql> update user set password=password('0000') where user=root;
ERROR 1054: Unknown column 'root' in 'where clause'
mysql> update user set password=password('0000') where user='root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

C:/>mysqladmin -u root status
Uptime: 1045  Threads: 1  Questions: 32  Slow queries: 0  Opens: 9  Flush tables: 1  Open table
s: 2  Queries per second avg: 0.031  Memory in use: 8332K  Max memory used: 8372K

C:/>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 9 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> exit
Bye

C:/>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 10 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> exit
Bye

C:/>mysqladmin -u root reload

C:/>mysql -u root
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)

C:/>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 17 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql>


創建一個新用戶:

mysql> show databases;
+----------+
| Database |
+----------+
| mydata   |
| mysql    |
| test     |
+----------+
3 rows in set (0.00 sec)

mysql> use mydata
Database changed
mysql> create table tab1(col1 int(4) not null primary key auto_increme
    -> col2 char(10) not null,
    -> col3 char(20) not null);
Query OK, 0 rows affected (0.05 sec)

mysql> grant all on mydata.* to [email protected] identified by 'bitan';
Query OK, 0 rows affected (0.02 sec)

mysql> use mysql;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_mysql |
+-----------------+
| columns_priv    |
| db              |
| func            |
| host            |
| tables_priv     |
| user            |
+-----------------+
6 rows in set (0.00 sec)

mysql> select host,name,password from user;
ERROR 1054: Unknown column 'name' in 'field list'
mysql> select host,user,password from user;
+-----------+-------+------------------+
| host      | user  | password         |
+-----------+-------+------------------+
| localhost | root  |                  |
| build     | root  |                  |
| localhost |       |                  |
| build     |       |                  |
| localhost | bitan | 000864941a82a3c6 |
+-----------+-------+------------------+
5 rows in set (0.00 sec)


限制用戶權限到表和列:

mysql> grant select on mydata.tab1 to tab1@localhost identified by 'tab1'
    -> ;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

C:/>mysql -u tab1 -p tab1
Enter password: ****
ERROR 1044: Access denied for user: 'tab1@localhost' to database 'tab1'

C:/>mysql -u tab1 -p mydata
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 11 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> select * from tab1;
+------+-------+--------------+
| col1 | col2  | col3         |
+------+-------+--------------+
|    1 | aaaaa | aaaaaaaaaaaa |
|    2 | bbbbb | bbbbbbbbbbbb |
+------+-------+--------------+
2 rows in set (0.00 sec)

mysql> insert tab1 (col2,col3) values('bbbbb', 'bbbbbbbbbbbb');
ERROR 1142: insert command denied to user: 'tab1@localhost' for table 'tab1'
mysql> exit
Bye

C:/>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 12 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> use mysql
Database changed
mysql> grant insert on mydata.tab1 to tab1@localhost
    -> ;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

C:/>mysql -u tab1 -p mydata
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 13 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> select * from tab1
    -> ;
+------+-------+--------------+
| col1 | col2  | col3         |
+------+-------+--------------+
|    1 | aaaaa | aaaaaaaaaaaa |
|    2 | bbbbb | bbbbbbbbbbbb |
+------+-------+--------------+
2 rows in set (0.00 sec)

mysql> insert tab1 (col2,col3) values('bbbbb', 'bbbbbbbbbbbb');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tab1;
+------+-------+--------------+
| col1 | col2  | col3         |
+------+-------+--------------+
|    1 | aaaaa | aaaaaaaaaaaa |
|    2 | bbbbb | bbbbbbbbbbbb |
|    3 | bbbbb | bbbbbbbbbbbb |
+------+-------+--------------+
3 rows in set (0.00 sec)
mysql> update tab1 set col2='cccc',col3='cccccccccccc' where col1=3
    -> ;
ERROR 1142: update command denied to user: 'tab1@localhost' for table 'tab1'
mysql> exit
Bye

C:/>mysql -u root mysql
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 14 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> grant update(col2) on mydata.tab1 to tab1@localhost;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

C:/>mysql -u tab1 -p mydata
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 15 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> update tab1 set col2='cccc',col3='cccccccccccc' where col1=3
    -> ;
ERROR 1143: UPDATE command denied to user: 'tab1@localhost' for column 'col3' in table 'tab1'
mysql> update tab1 set col2='cccc' where col1=3
    -> ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tab1;
+------+-------+--------------+
| col1 | col2  | col3         |
+------+-------+--------------+
|    1 | aaaaa | aaaaaaaaaaaa |
|    2 | bbbbb | bbbbbbbbbbbb |
|    3 | cccc  | bbbbbbbbbbbb |
+------+-------+--------------+
3 rows in set (0.00 sec)

mysql> exit
Bye

C:/>mysql -u root mysql
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 16 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> grant update on mydata.tab1 to tab1@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

C:/>mysql -u tab1 -p mydata
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 17 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> update tab1 set col2='cccc',col3='cccccccccccc' where col1=3
    -> ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tab1;
+------+-------+--------------+
| col1 | col2  | col3         |
+------+-------+--------------+
|    1 | aaaaa | aaaaaaaaaaaa |
|    2 | bbbbb | bbbbbbbbbbbb |
|    3 | cccc  | cccccccccccc |
+------+-------+--------------+
3 rows in set (0.00 sec)

mysql>

 

一個用戶將自己所擁有的權限賦給另一個用戶:

C:/>mysql -u root mysql
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 26 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> grant select on mydata.* to user6@localhost identified by 'x' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> grant insert on mydata.* to user7@localhost identified by 'x' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

C:/>mysql -u user6 -p
Enter password: *
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 27 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> grant select on mydata.* to user7@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> grant update on mydata.* to user8@localhost identified by 'x';
ERROR 1044: Access denied for user: 'user6@localhost' to database 'mydata'
mysql> grant update on mydata.* to user7@localhost;
ERROR 1044: Access denied for user: 'user6@localhost' to database 'mydata'
mysql> grant all on *.* to user7@localhost;
ERROR 1045: Access denied for user: 'user6@localhost' (Using password: YES)
mysql> grant all on mydata.* to user7@localhost;
ERROR 1044: Access denied for user: 'user6@localhost' to database 'mydata'
mysql>

 

取消用戶權限:

mysql> grant select,insert,update on mydata.tab1 to user6@localhost identified by 'x';
Query OK, 0 rows affected (0.06 sec)

mysql> revoke update on mydata.tab1 from user6@localhost;
Query OK, 0 rows affected (0.03 sec)

mysql> revoke insert on mydata.tab1 from user6@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> revoke all on mydata.tab1 from user6@localhost;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

C:/>mysql -u user6 -p
Enter password: *
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 55 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> use mydata;
ERROR 1044: Access denied for user: 'user6@localhost' to database 'mydata'
mysql> exit
Bye

C:/>mysql -u user6 -p mydata;
Enter password: *
ERROR 1044: Access denied for user: 'user6@localhost' to database 'mydata;'


刪除一個用戶:

C:/>mysql -u root mysql
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 62 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> select host,user from user;
+-----------+----------------+
| host      | user           |
+-----------+----------------+
| %         | user2localhost |
| build     |                |
| build     | root           |
| localhost |                |
| localhost | root           |
| localhost | user1          |
| localhost | user3          |
| localhost | user4          |
| localhost | user5          |
| localhost | user6          |
| localhost | user7          |
+-----------+----------------+
11 rows in set (0.02 sec)

mysql> delete from user where user='user7';
Query OK, 1 row affected (0.00 sec)

mysql> delete from user where user='user6';
Query OK, 1 row affected (0.00 sec)

mysql> delete from user where user='user5';
Query OK, 1 row affected (0.00 sec)

mysql> delete from user where user='user4';
Query OK, 1 row affected (0.00 sec)

mysql> delete from user where user='user3';
Query OK, 1 row affected (0.00 sec)

mysql> delete from user where user='user2';
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user from user;
+-----------+----------------+
| host      | user           |
+-----------+----------------+
| %         | user2localhost |
| build     |                |
| build     | root           |
| localhost |                |
| localhost | root           |
| localhost | user1          |
+-----------+----------------+
6 rows in set (0.00 sec)

mysql>

 

使用mysqldump備份一個數據庫:

mysql> grant all on mydata.tab1 to user2@localhost identified by 'x';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

C:/>mysqldump -u user2 mydata > ./mydata.sql
mysqldump: Got error: 1045: Access denied for user: 'user2@localhost' (Using password: NO) when
 trying to connect

C:/>mysqldump -u user2 -p mydata > ./mydata.sql
Enter password: *


C:/>mysql -u root mydata
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 68 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> create table tab2 (col1 int not null primary key auto_increment,
    -> col2 char(20) not null);
Query OK, 0 rows affected (0.03 sec)

mysql> exit
Bye

C:/>mysqldump -u user2 -p mydata > ./mydata.sql
Enter password: *

C:/>mysql -u root mysql
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 70 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> grant all on mydata.tab2 to user2@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

C:/>mysqldump -u user2 -p mydata > ./mydata.sql
Enter password: *

C:/>
////////////////////////////////////////////////
C:/mydata.sql:
-- MySQL dump 9.11
--
-- Host: localhost    Database: mydata
-- ------------------------------------------------------
-- Server version 4.0.20a-debug

--
-- Table structure for table `tab1`
--

CREATE TABLE tab1 (
  col1 int(4) NOT NULL auto_increment,
  col2 char(10) NOT NULL default '',
  col3 char(20) NOT NULL default '',
  PRIMARY KEY  (col1)
) TYPE=MyISAM;

--
-- Dumping data for table `tab1`
--

INSERT INTO tab1 VALUES (1,'aaaaa','aaaaaaaaaaaa');
INSERT INTO tab1 VALUES (2,'bbbbb','bbbbbbbbbbbb');
INSERT INTO tab1 VALUES (3,'cccc','cccccccccccc');
INSERT INTO tab1 VALUES (4,'ccccc','cccccccccccc');

--
-- Table structure for table `tab2`
--

CREATE TABLE tab2 (
  col1 int(11) NOT NULL auto_increment,
  col2 char(20) NOT NULL default '',
  PRIMARY KEY  (col1)
) TYPE=MyISAM;

--
-- Dumping data for table `tab2`
--
////////////////////////////////////////////////

 

 

使用mysqldump備份一個或多個表:

C:/>mysqldump -u user2 mydata tab1 -p > ./mydata.sql
Enter password: *

C:/>mysqldump -u user2 mydata tab1 tab2 -p > ./mydata.sql
Enter password: *

C:/>

 

從備份文件恢復一個數據庫:

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

mysql> show tables;
Empty set (0.00 sec)

mysql> exit
Bye

C:/>mysql -u root mydata < ./mydata.sql

C:/>mysql -u root mydata
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 91 to server version: 4.0.20a-debug

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> show tables;
+------------------+
| Tables_in_mydata |
+------------------+
| tab1             |
| tab2             |
+------------------+
2 rows in set (0.00 sec)

mysql>


 

 

發佈了75 篇原創文章 · 獲贊 3 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章