mysql4


mysql> help lock;
Name: 'LOCK'
Description:
Syntax:
LOCK TABLES
    tbl_name [[AS] alias] lock_type
    [, tbl_name [[AS] alias] lock_type] ...

lock_type:
    READ [LOCAL]
  | [LOW_PRIORITY] WRITE

UNLOCK TABLES

读锁
mysql> create table c1(id int);
mysql> insert into c1 values(1),(2),(3),(4);
mysql> lock table c1 read;
mysql> insert into c1 values(5);
ERROR 1099 (HY000): Table 'c1' was locked with a READ lock and can't be updated

mysql> delete from c1 where id=4;
ERROR 1099 (HY000): Table 'c1' was locked with a READ lock and can't be updated

在另一个终端 输入mysql
mysql> use db1;
mysql> insert into c1 values(5);
锁住
回到第一个终端,解锁
mysql> unlock tables;

写锁
mysql> lock table c1 write;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from c1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
+------+
5 rows in set (0.00 sec)

mysql> delete from c1 where id=5;

mysql> insert into c1 values(6);
Query OK, 1 row affected (0.00 sec)

在另一个终端
mysql> select * from c1;
锁住

回到第一个终端,解锁
mysql> unlock tables;

----------------------------------
myisam

mysql> create table c3(id int) engine=myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into c3 values(1),(2),(3),(4);

# ls /data/mysql/db1
c3.frm    c3.MYD    c3.MYI 

mysql> alter table c3 engine=innodb;

# ls /data/mysql/db1
c3.frm   

默认存储引擎
# vim /etc/my.cnf

[mysqld]
default-storage-engine = myisam

# service mysqld restart

# mysql
mysql> use db1;

mysql> create table c4(id int);
Query OK, 0 rows affected (0.04 sec)

mysql> show create table c4;
+-------+--------------------------------------------------------------------------------------+
| Table | Create Table                                                                         |
+-------+--------------------------------------------------------------------------------------+
| c4    | CREATE TABLE `c4` (
  `id` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

启动时临时生效
# mysqld_safe --default-storage-engine=myisam --user=mysql &

-------------------------------------
memory 引擎

mysql> create table c6(id int) engine=memory;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into c6 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from c6;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+

# service mysqld restart
# mysql

mysql> use db1;
Database changed
mysql> select * from c6;
Empty set (0.00 sec)

mysql> desc c6;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

----------------------------------------------
用户的权限管理

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.02 sec)

mysql> use mysql;
mysql> show tables;
mysql> desc user;

mysql> select user,password,host from user;
+------+----------+--------------------+
| user | password | host               |
+------+----------+--------------------+
| root |          | localhost          |
| root |          | teacher.uplook.com |
| root |          | 127.0.0.1          |
| root |          | ::1                |
|      |          | localhost          |
|      |          | teacher.uplook.com |
+------+----------+--------------------+

# mysqladmin -u root password 123

mysql> select user,password,host from user;
+------+-------------------------------------------+--------------------+
| user | password                                  | host               |
+------+-------------------------------------------+--------------------+
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | localhost          |
| root |                                           | teacher.uplook.com |
| root |                                           | 127.0.0.1          |
| root |                                           | ::1                |
|      |                                           | localhost          |
|      |                                           | teacher.uplook.com |
+------+-------------------------------------------+--------------------+

# service mysqld restart

# mysql -u root -p
Enter password: 

忘掉密码
# service mysqld stop

跳过授权表的方式启动mysql

# mysqld_safe --skip-grant-tables --user=mysql &
# mysql
mysql> use mysql;
修改密码
mysql> update user set password=password("") where user="root" and host="localhost";

mysql> select user,password,host from user;
+------+----------+--------------------+
| user | password | host               |
+------+----------+--------------------+
| root |          | localhost          |
| root |          | teacher.uplook.com |
| root |          | 127.0.0.1          |
| root |          | ::1                |
|      |          | localhost          |
|      |          | teacher.uplook.com |
+------+----------+--------------------+
重启服务
# service mysqld restart
# mysql
mysql> 

-------------------------------
创建用户

mysql> use mysql;

mysql> create user mm@localhost identified by "mm";
mysql> select user,password,host from user;
+------+-------------------------------------------+--------------------+
| user | password                                  | host               |
+------+-------------------------------------------+--------------------+
| root |                                           | localhost          |
| root |                                           | teacher.uplook.com |
| root |                                           | 127.0.0.1          |
| root |                                           | ::1                |
|      |                                           | localhost          |
|      |                                           | teacher.uplook.com |
| mm   | *D2757F592F75F56A5B5FAA2FE99D350154374276 | localhost          |
+------+-------------------------------------------+--------------------+

删除用户
mysql> drop user mm@localhost;
Query OK, 0 rows affected (0.03 sec)

mysql> select user,password,host from user;
+------+----------+--------------------+
| user | password | host               |
+------+----------+--------------------+
| root |          | localhost          |
| root |          | teacher.uplook.com |
| root |          | 127.0.0.1          |
| root |          | ::1                |
|      |          | localhost          |
|      |          | teacher.uplook.com |
+------+----------+--------------------+

******************
# vim /etc/my.cnf
 26 [mysqld]
 27 skip-name-resolve
# service mysqld restart
******************

授权
grant
mysql> grant 权限 on 对象  to  用户@主机名  identified by “密码”


mysql> grant all on *.* to [email protected] identified by "123";
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


mysql> grant all on db1.* to "u1"@"192.168.3.19" identified by "123";
Query OK, 0 rows affected (0.02 sec)

mysql> grant select on db1.* to "u2"@"192.168.3.21" identified by "123";
Query OK, 0 rows affected (0.00 sec)

创建一个数据库
mysql> create database db2;
Query OK, 1 row affected (0.01 sec)

mysql> use db2;
Database changed

mysql> create table z1(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into z1 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> grant select,update on db2.z1 to "u3"@"192.168.3.27" identified by "123";
Query OK, 0 rows affected (0.00 sec)

mysql> create table z2(id int,name char(11));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into z2 values(1,"tom"),(2,"mike"),(3,"john");

mysql> grant select(id),update(id) on db2.z2 to "u4"@"192.168.3.34" identified by "123";

mysql> flush privileges;

全局权限
mysql> select  user,password,host,select_priv from user;
+----------+-------------------------------------------+--------------------+-------------+
| user     | password                                  | host               | select_priv |
+----------+-------------------------------------------+--------------------+-------------+
| root     |                                           | localhost          | Y           |
| root     |                                           | teacher.uplook.com | Y           |
| root     |                                           | 127.0.0.1          | Y           |
| root     |                                           | ::1                | Y           |
|          |                                           | localhost          | N           |
|          |                                           | teacher.uplook.com | N           |
| chengbo  | *26288A04AC9B9DBFE0770AB6BEA4D06F013EF048 | 192.168.3.229      | N           |
| tom      | *71FF744436C7EA1B954F6276121DB5D2BF68FC07 | 192.168.3.2        | N           |
| yongjian | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 192.168.3.12       | Y           |
| u1       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 192.168.3.19       | N           |
| u2       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 192.168.3.21       | N           |
| u3       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 192.168.3.27       | N           |
| u4       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 192.168.3.34       | N           |
+----------+-------------------------------------------+--------------------+-------------+


保存数据库的权限
mysql> select Select_priv,user,db,host from db;
+-------------+------+---------+--------------+
| Select_priv | user | db      | host         |
+-------------+------+---------+--------------+
| Y           |      | test    | %            |
| Y           |      | test\_% | %            |
| Y           | u1   | db1     | 192.168.3.19 |
| Y           | u2   | db1     | 192.168.3.21 |
+-------------+------+---------+--------------+

保存表的权限
mysql> select host,db,user,table_name,Table_priv from tables_priv;
+--------------+-----+------+------------+---------------+
| host         | db  | user | table_name | Table_priv    |
+--------------+-----+------+------------+---------------+
| 192.168.3.27 | db2 | u3   | z1         | Select,Update |
| 192.168.3.34 | db2 | u4   | z2         |               |
+--------------+-----+------+------------+---------------+

保存列的权限
mysql> select * from columns_priv;
+--------------+-----+------+------------+-------------+---------------------+---------------+
| Host         | Db  | User | Table_name | Column_name | Timestamp           | Column_priv   |
+--------------+-----+------+------------+-------------+---------------------+---------------+
| 192.168.3.34 | db2 | u4   | z2         | id          | 2013-09-04 13:43:02 | Select,Update |
+--------------+-----+------+------------+-------------+---------------------+---------------+

-------------------------
收回权限

删除用户

revoke 权限列表 on  对象  from  用户@主机名;

mysql> revoke all on *.* from [email protected];

mysql> show grants for [email protected];
+--------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.3.2' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |
+--------------------------------------------------------------------------------------------------------------+

查看u3权限
mysql> show grants for [email protected];
+--------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'u3'@'192.168.3.27' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT SELECT, UPDATE ON `db2`.`z1` TO 'u3'@'192.168.3.27'                                                    |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

收回update权限
mysql> revoke update on db2.z1 from [email protected];
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for [email protected];
+--------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'u3'@'192.168.3.27' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT SELECT ON `db2`.`z1` TO 'u3'@'192.168.3.27'                                                            |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

------------------------------
复制表

mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> use db1;
Database changed

全表复制
mysql> create table shop44 select * from shop33;

mysql> create table user1 select * from mysql.user;
Query OK, 13 rows affected (0.01 sec)
Records: 13  Duplicates: 0  Warnings: 0

复制表的结构

mysql> create table shop55 select * from shop33 where 2=3;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from shop55;
Empty set (0.00 sec)

mysql> desc shop55;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(50) | YES  |     | NULL    |       |
| price  | float(10,2) | YES  |     | NULL    |       |
| street | varchar(30) | YES  |     | NULL    |       |
| city   | varchar(30) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

有条件的复制

mysql> create table shop66 select * from shop33 where id>500000;
Query OK, 161907 rows affected (1.98 sec)
Records: 161907  Duplicates: 0  Warnings: 0

mysql> select * from shop66 limit 0,5;
+--------+------------+-----------+--------------+------------+
| id     | name       | price     | street       | city       |
+--------+------------+-----------+--------------+------------+
| 500001 | name500001 | 500001.00 | street500001 | city500001 |
| 500002 | name500002 | 500002.00 | street500002 | city500002 |
| 500003 | name500003 | 500003.00 | street500003 | city500003 |
| 500004 | name500004 | 500004.00 | street500004 | city500004 |
| 500005 | name500005 | 500005.00 | street500005 | city500005 |
+--------+------------+-----------+--------------+------------+

插入数据时复制表的内容

mysql> insert into shop55 select * from shop33 where id>600000;

mysql> select * from shop55 limit 0,5;
+--------+------------+-----------+--------------+------------+
| id     | name       | price     | street       | city       |
+--------+------------+-----------+--------------+------------+
| 600001 | name600001 | 600001.00 | street600001 | city600001 |
| 600002 | name600002 | 600002.00 | street600002 | city600002 |
| 600003 | name600003 | 600003.00 | street600003 | city600003 |
| 600004 | name600004 | 600004.00 | street600004 | city600004 |
| 600005 | name600005 | 600005.00 | street600005 | city600005 |
+--------+------------+-----------+--------------+------------+

mysql> delete from shop55;
Query OK, 495256 rows affected (3.61 sec)

mysql> insert into shop55(id,name) select id,name from shop33 where id>600000; 
Query OK, 61907 rows affected (1.59 sec)
Records: 61907  Duplicates: 0  Warnings: 0

mysql> select * from shop55 limit 0,5;
+--------+------------+-------+--------+------+
| id     | name       | price | street | city |
+--------+------------+-------+--------+------+
| 600001 | name600001 |  NULL | NULL   | NULL |
| 600002 | name600002 |  NULL | NULL   | NULL |
| 600003 | name600003 |  NULL | NULL   | NULL |
| 600004 | name600004 |  NULL | NULL   | NULL |
| 600005 | name600005 |  NULL | NULL   | NULL |
+--------+------------+-------+--------+------+

--------------------------
数据的导入和导出

# vim /test/m1     //每一列用tab键隔开

cb      60      80
yj      70      75
pl      75      85


mysql> use db1;
Database changed

mysql> create table cj(name varchar(31),ule int,ula int);

mysql> load data infile "/test/m1" into table cj;
Query OK, 4 rows affected, 8 warnings (0.05 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 8

mysql> select * from cj;
+------+------+------+
| name | ule  | ula  |
+------+------+------+
| cb   |   60 |   80 |
| yj   |   70 |   75 |
| pl   |   75 |   85 |
+------+------+------+

mysql> select name,ule from cj where ule=(select max(ule) from cj);
+------+------+
| name | ule  |
+------+------+
| pl   |   75 |
+------+------+

# vim /test/m1
cb,60,80
yj,70,75
pl,75,85

mysql> load data infile "/test/m1" into table cj fields terminated by ",";

mysql> select * from cj;
+------+------+------+
| name | ule  | ula  |
+------+------+------+
| cb   |   60 |   80 |
| yj   |   70 |   75 |
| pl   |   75 |   85 |
| cb   |   60 |   80 |
| yj   |   70 |   75 |
| pl   |   75 |   85 |
+------+------+------+

# vim /test/m1
tttcb,60,80
hello tttyj,70,75
good morning tttpl,75,85

mysql> truncate cj;

mysql> load data infile "/test/m1" into table cj fields terminated by "," lines starting by "ttt";
Query OK, 3 rows affected (0.02 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from cj;
+------+------+------+
| name | ule  | ula  |
+------+------+------+
| cb   |   60 |   80 |
| yj   |   70 |   75 |
| pl   |   75 |   85 |
+------+------+------+
-------------------------------
数据导出

# chmod 777 /test  //mysql用户对目录必须有写权限

mysql> select * from cj into outfile "/test/m3";

[root@teacher ~]# cat /test/m3
cb      60      80
yj      70      75
pl      75      85

--------------------------
备份和恢复

热备份
mysqldump

单库备份
mysql> create database db3;
Query OK, 1 row affected (0.01 sec)

mysql> use db3;
Database changed

mysql> create table d1(id int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into d1 values(1),(2),(3);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0
备份
# mysqldump db3 -u root -p > /test/db3.sql
Enter password: 
删除db3
mysql> drop database db3;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
恢复
mysql> create database db3;
Query OK, 1 row affected (0.00 sec)

mysql> use db3;
Database changed
mysql> source /test/db3.sql

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

mysql> select * from d1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+

多库备份

# mysqldump -B mysql db2 -u root -p > /tmp/db2_mysql.sql

[root@teacher mysql]# mysql -u root -p </tmp/db2_mysql.sql 
Enter password: 
------------
全库备份

# mysqldump --all-databases -u root -p > /test/all.sql
恢复
# mysql -u root -p < /test/all.sql
-----------------
备份数据库中的某些表

# mysqldump mysql user db -u root -p >/test/mysql.user_db.sql

mysql> use mysql

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

mysql> drop table db;

恢复
# mysql mysql -u root -p < /test/mysql.user_db.sql
----------------
加锁

# mysqldump --all-databases --lock-all-tables -u root -p>/test/all.sql

# mysqldump mysql user db --lock-tables -u root -p > /test/mysql.user_db.sql


 

你可能感兴趣的:(mysql)