Heim  >  Artikel  >  Datenbank  >  MySQL 数据库简单操作

MySQL 数据库简单操作

WBOY
WBOYOriginal
2016-06-07 16:43:56885Durchsuche

对于想要从事或爱好mysql相关工作的童鞋们,有必要掌握在命令行下对mysql实现一些简单的操作。本文从描述了如何登录到mysql数据库

对于想要从事或爱好mysql相关工作的童鞋们,有必要掌握在命令行下对mysql实现一些简单的操作。本文从描述了如何登录到mysql数据库服务器,如何在mysql提示符下发布命令,创建数据库,,以及执行一些简单的DML操作。

1、连接到与退出mysql

为了连接mysql数据库服务器,当调用mysql时,通常需要提供一个MySQL用户名并且很可能需要一个密码。如果服务器
运行在登录服务器之外的其它机器上,还需要指定主机名。联系管理员以找出进行连接所使用的参数 (即,连接的主机
、用户名和使用的密码)。知道正确的参数后,可以按照以下方式进行连接:
shell> mysql -h host -u user -p
mysql> select version(),current_date;
+---------------------------------------+--------------+
| version()                            | current_date |
+---------------------------------------+--------------+
| 5.6.17-enterprise-commercial-advanced | 2014-04-28  |
+---------------------------------------+--------------+
1 row in set (0.03 sec)

--在允许匿名登录到本地服务器的情下可以直接在shell提示符下直接输入mysql即可实现登录
mysql> #提示符告诉你mysql准备为你输入命令。

shell> mysql
--输入分号表示结束命令输入并执行该命令
--成功地连接后,可以在mysql>提示下输入QUIT (或\q ,exit)随时退出
mysql> QUIT
Bye
--在Unix中,也可以按control-D键断开服务器。

--------------------------------------分割线 --------------------------------------

Ubuntu 14.04下安装MySQL

《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主从服务器

Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二进制安装

--------------------------------------分割线 --------------------------------------

2、发布命令

mysql执行命令可分为非交互与交互模式
a) 非交互模式
非交互模式,也叫批模式,也就是将想要运行的命令放在一个文件中,然后告诉mysql从文件读取它的输入。
通常用于返回数据量较大,以及批量管理,执行特殊脚本运行的情形。
shell> mysql

[root@linux1 ~]# more query.sql
show databases;
use cnfo
select * from tb_tmp;
[root@linux1 ~]# mysql -u root -pmysql Warning: Using a password on the command line interface can be insecure.
Database
information_schema
cnfo
mysql
performance_schema
test
name    sex    birth
Jack    F      2014-04-28
John    M      2013-04-28

--也可以使用下面的2种方式来执行批
mysql > source /

/filename
mysql > \.//finename     

--如下面的演示
[root@linux1 ~]# mysql -u root -pmysql
mysql> source query.sql
+--------------------+
| Database          |
+--------------------+
| information_schema |
| cnfo              |
| mysql              |
| performance_schema |
| test              |
+--------------------+
5 rows in set (0.00 sec)

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
+------+------+------------+
| name | sex  | birth      |
+------+------+------------+
| Jack | F    | 2014-04-28 |
| John | M    | 2013-04-28 |
+------+------+------------+
2 rows in set (0.00 sec)

也可以在shell模式下直接执行SQL,如下面的方法:
-e or --execution=option 
shell>mysql -e "SQL cmd1;SQL cmd2;.."
shell>mysql --execute="SQL cmd1;SQL cmd2;.."

b) 交互模式
交互模式就是直接在mysql提示符下发布命令并执行命令。
如下操作,不区分大小写,输入回车后会得到命令执行的结果,即为交互模式。
mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;

--简单计算
mysql> select power(2,3),(5-1)*4;
+------------+---------+
| power(2,3) | (5-1)*4 |
+------------+---------+
|          8 |      16 |
+------------+---------+
1 row in set (0.00 sec)

--分号分割多行
mysql> select version();select current_date;
+---------------------------------------+
| version()                            |
+---------------------------------------+
| 5.6.17-enterprise-commercial-advanced |
+---------------------------------------+
1 row in set (0.01 sec)

+--------------+
| current_date |
+--------------+
| 2014-04-28  |
+--------------+
1 row in set (0.00 sec)

--换行输入命令
--注,可以输入空行
mysql> select user(),
    -> current_date;
+----------------+--------------+
| user()        | current_date |
+----------------+--------------+
| root@localhost | 2014-04-28  |
+----------------+--------------+
1 row in set (0.00 sec)

--取消执行当前命令
mysql> select current_date()\c

更多详情见请继续阅读下一页的精彩内容:

linux

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn