您的位置:首页 > 运维架构 > Linux

Linux 下MySQL的基本操作

2017-11-26 18:11 417 查看
1.连接MySQL
连接到本机的MySQL
键入命令mysql -u root -p 回车提示你输入密码。

Ps:用户名前可以有空格也可以没有空格,但是密码前必须没有空格

MySQL的提示符是: mysql>

 

连接到远程主机上的MySQL

格式: mysql -h主机名 -u用户名 -p用户密码

假设远程主机的IP为:192.168.8.113,用户名为sd 密码为:123456

则可以键入以下命令:

  mysql> mysql -h192.168.8.113 -u sd -p 123456(u和sd之间可以不加空格,其他也一样)

 

2. 退出Mysql命令

  mysql>exit(回车)

 

3. 显示数据库

  mysql>show databases;

 

4. 选择数据库

  mysql>use 数据库名;

 

5. 显示数据库中的数据表

  mysql>show tables;

 

6. 显示数据表的结构

  mysql>describe 数据表名

 

7. 建立数据库

  mysql>create 数据库名

 

8. 建立数据表

  mysql>create table 数据表名

 

1 create table 表名 (字段设定列表);
2
3 mysql> create table name(
4
5     -> id int auto_increment not null primary key ,
6
7     -> uname char(8),
8
9     -> gender char(2),
10
11     -> birthday date );
12
13 Query OK, 0 rows affected (0.03 sec)
14
15
16
17 mysql> show tables;
18
19 +------------------+
20
21 | Tables_in_userdb |
22
23 +------------------+
24
25 | name             |
26
27 +------------------+
28
29 row in set (0.00 sec)
30
31
32
33 mysql> describe name;
34
35 +----------+---------+------+-----+---------+----------------+
36
37 | Field    | Type    | Null | Key | Default | Extra          |
38
39 +----------+---------+------+-----+---------+----------------+
40
41 | id       | int(11) | NO   | PRI | NULL    | auto_increment |
42
43 | uname    | char(8) | YES  |     | NULL    |                |
44
45 | gender   | char(2) | YES  |     | NULL    |                |
46
47 | birthday | date    | YES  |     | NULL    |                |
48
49 +----------+---------+------+-----+---------+----------------+
50
51 rows in set (0.00 sec)
52
53
54
55 注: auto_increment 自增
56
57      primary key    主键


 

9. 删除数据库

  mysql>drop database 数据库名

  删除表

    drop table 表名

  删除记录

    delete from name where uname = ‘张三

10. 增加记录

  insert into name(uname,gender)

  update name set birthday = ‘1996-05-16’where uname = ‘张三’

 

11. 显示表中的记录

  mysql>select * from 数据表名

 

12. 往表中插入记录

  mysql>insert into 数据表名 values(pair)

 

13. 更新表中记录

  mysql>update 数据表名 set 字段名1='a', 字段名='b',..., where ...

 

14. 命令行修改root密码

  mysql>update mysql.user set password=PASSWORD(’新密码’) where user=’root’;

  mysql>FLUSH PRIVILEGES;

 

15. 备份数据库

  mysql>mysqldump -u root -p --opt 数据库名>备份名; //进入到库目录

 

16. 恢复数据库

  mysql>mysql -u root -p 数据库名<备份名; //恢复时数据库必须存在,可以为空数据库

 

参考连接:http://www.cnblogs.com/xdpxyxy/archive/2012/11/16/2773662.html

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: