原文同步至 http://www.waylau.com/mysql-tutorial/
MySQL 是最流行的开源数据库。
本文简明的讲解了 MySQL 如何下载安装到使用的整个过程。
MySQL 支持多种特性:
下载地址:http://www.mysql.com/downloads/
本例用的是 MySQL Community Edition (GPL) 版本,开源免费。
安装方式,可以选择 MySQL Installer MSI 或者是 编译压缩包(形如 mysql-5.6.26-win32.zip 或 mysql-5.6.26-winx64.zip)。
如果是 MySQL Installer MSI 方式安装,请参阅 http://www.waylau.com/mysql-5-6-13-windows-platform-installation-use-graphic/。本教程采用 编译压缩包 的安装形式。
下载完成后,解压到放到任意目录,本例为 C:\mysql
目录
放到 Path 变量下增加 ,C:\mysql\bin
复制安装目录下的 my-default.ini 并重命名为 my.ini ,修改为
[mysqld]
basedir=C:/mysql
datadir=D:/MysqlData/data
basedir 为 mysql 安装目录。datadir 为数据存放目录
安装作为 Windows 服务。
MySQL 会随着 Windows 的启动和关闭而启动和关闭
mysqld –install
C:\Users\Administrator.USER-20150424OX>mysqld –install
Service successfully installed.
其他可选的有:设置为手动启动或者关闭服务
mysqld –install-manual
移除服务
mysqld –remove
启动
NET START MySQL
C:\Users\Administrator.USER-20150424OX>NET START MySQL
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
关闭
NET STOP MySQL
C:\Users\Administrator.USER-20150424OX>NET STOP MySQL
MySQL 服务正在停止.
MySQL 服务已成功停止。
mysqlshow
C:\Users\Administrator.USER-20150424OX>mysqlshow
+--------------------+
| Databases |
+--------------------+
| information_schema |
| test |
+--------------------+
mysqlshow -u root mysql
C:\Users\Administrator.USER-20150424OX>mysqlshow -u root mysql
Database: mysql
+---------------------------+
| Tables |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
mysqladmin version status proc
C:\Users\Administrator.USER-20150424OX>mysqladmin version status proc
mysqladmin Ver 8.42 Distrib 5.6.24, for Win64 on x86_64
Copyright (c) 2000, 2015, 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.
Server version 5.6.24
Protocol version 10
Connection localhost via TCP/IP
TCP port 3306
Uptime: 9 min 12 sec
Threads: 1 Questions: 7 Slow queries: 0 Opens: 67 Flush tables: 1 Open tabl
es: 60 Queries per second avg: 0.012
Uptime: 552 Threads: 1 Questions: 8 Slow queries: 0 Opens: 67 Flush tables:
1 Open tables: 60 Queries per second avg: 0.014
mysqladmin: process list failed; error: 'Access denied; you need (at least one o
f) the PROCESS privilege(s) for this operation'
mysql test
C:\Users\Administrator.USER-20150424OX>mysql test
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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 –help
连接到服务器,其中 host 是服务器IP, user 是用户名
mysql -h host -u user -p
如果服务器在本地 ,
mysql -u user –p
提示输入密码,验证通过后,如下
C:\Users\Administrator.USER-20150424OX>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
断开与服务器的链接
QUIT
mysql> QUIT
Bye
查询(命令语句是不区分大小写)
mysql> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 5.6.24 | 2015-05-10 |
+-----------+--------------+
1 row in set (0.01 sec)
列出所有数据库
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
创建一个自己的数据库 menagerie
mysql> CREATE DATABASE menagerie;
Query OK, 1 row affected (0.03 sec)
给自己授予管理员权限
GRANT ALL ON menagerie.* TO 'your_mysql_name'@'your_client_host';
访问数据库 menagerie
mysql> USE menagerie
Database changed
列出数据库表
mysql> SHOW TABLES;
Empty set (0.00 sec)
创建表 pet
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Query OK, 0 rows affected (0.43 sec)
查看 pet 表的详情
mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
插入数据
mysql> INSERT INTO pet
-> VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
Query OK, 1 row affected (0.04 sec)
查询所有的数据
mysql> SELECT * FROM pet;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)
修改数据
mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Puffball';
Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0
删除数据
mysql> DELETE FROM pet;
Query OK, 1 row affected (0.04 sec)