SpongeBob

人生三种境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

Linux下MySQL的简单使用(一)

       

        MySQL是一个关系型数据库管理系统,由MySQL AB 公司开发,目前属于 Oracle 。MySQL 是最流行的关系型数据库管理系统之一。关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。拥有体积小、速度快、总体成本低,尤其是开放源码这一特点。

 
一.登陆MySQL
 
在没有设置密码的前提下,输入命令mysql可直接进入MySQL界面;
[root@centos6 ~]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@centos6 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

 

那么如何增加MySQL安全性呢?那就需要设置密码了。
 
[root@centos6 ~]# mysqladmin -uroot password 'centos'
[root@centos6 ~]# mysql -uroot -pcentos
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
 
二.MySQL的重要目录
 

MySQL 安装完成后,它的数据库文件、配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Linux的初学者,因为 Linux本身的目录结构就比较复杂,如果搞不清楚MySQL的安装目录那就无从谈起深入学习。  

下面就介绍一下这几个目录。  

1、 数据库目录

/var/lib/mysql/  

2、配置文件

/usr/share /mysql(mysql.server命令及配置文件)  

3、相关命令  

/usr/bin(mysqladmin mysqldump等命令)  

4、启动脚本  

/etc/rc.d/init.d/(启动脚本文件mysql的目录)

 

三·.基本的MySQL操作命令
 
1.显示数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

Mysql刚安装完有两个数据库:mysql和test。mysql库非常重要, 它里面有MySQL的系统信息,我们改密码和新增用户,实际上就是用这个库中的相关表进行操作。

 

2.显示数据库中的表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> use hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.00 sec)

 

3.显示数据表的字段结构

mysql> describe classes;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| ClassID  | tinyint(3) unsigned  | NO   | PRI | NULL    | auto_increment |
| Class    | varchar(100)         | YES  |     | NULL    |                |
| NumOfStu | smallint(5) unsigned | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

也可以使用两一条命令,显示比这个更详细,而且可以把建表语句全部列出来:

mysql> show create table classes\G'
*************************** 1. row ***************************
       Table: classes
Create Table: CREATE TABLE `classes` (
  `ClassID` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `Class` varchar(100) DEFAULT NULL,
  `NumOfStu` smallint(5) unsigned DEFAULT NULL,
  PRIMARY KEY (`ClassID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

 

4.显示某个表中的记录

mysql> select * from classes;
+---------+----------------+----------+
| ClassID | Class          | NumOfStu |
+---------+----------------+----------+
|       1 | Shaolin Pai    |       10 |
|       2 | Emei Pai       |        7 |
|       3 | QingCheng Pai  |       11 |
|       4 | Wudang Pai     |       12 |
|       5 | Riyue Shenjiao |       31 |
|       6 | Lianshan Pai   |       27 |
|       7 | Ming Jiao      |       27 |
|       8 | Xiaoyao Pai    |       15 |
+---------+----------------+----------+
8 rows in set (0.01 sec)

 

5.查询当前是哪个用户

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

 

6.创建一个新的数据库

mysql> create database hahaha;
Query OK, 1 row affected (0.00 sec)

 

7.创建一个新表

mysql> use hahaha;
Database changed
mysql> create table t1 (`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.04 sec)

字段名需要用引号括起来

 

8.查询语句

mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
|       25 |
+----------+
1 row in set (0.00 sec)

students表示mysql库的表;count(*)表示表中共有多少行。

 

9.删除指定表

 

mysql> use hahaha;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table t1;
Query OK, 0 rows affected (0.00 sec)

 

 

        这些只是一些简单的数据库操作,用来帮助我们i了解数据库和能简单地对数据库进行操作。在下一篇,我们会更深入了解数据库的更多使用!

 
 
 
 
 
 
 
 

posted on 2018-09-22 16:30  青山揽流萤  阅读(597)  评论(0编辑  收藏  举报

导航