mysql数据库的学习

Mysql 数据库

MySQL 是一个真正的多用户、多线程 SQL 数据库服务器。 SQL (结构化查询语言)是世界上最流行的和标准化的数据库语言。 MySQL 是以一个客户机 / 服务器结构的实现,它由一个服务器守护程序 mysqld 和很多不同的客户程序和库组成。并且能够支持多平台。

各种版本下载地址: http://dev.mysql.com/downloads/

学习资料: http://dev.mysql.com/doc/ MySQL Reference Manual (mysql 参考手册 )

可视化管理工具: http://dev.mysql.com/downloads/other/mysqlcc.html

在这里使用的是 4.1.12 版本,当前最高版本是 5.0.4-beta 。

1 、数据库的安装:

(1) 、 windowXp 下安装 4.1.12 直接点击下载的 exe 文件就可以安装了。

(2) 、 RedHat Enterprise Linux 4 (x86) 下安装 server4.1.12 和 client4.1.12

shell> rpm -i MySQL-server-standard- 4.1.12 -0.rhel4.i386.rpm

shell> rpm -i MySQL-client-standard- 4.1.12 -0.rhel4.i386.rpm


注: RedHat Enterprise Linux 4 软件包自带 4.1.7

2 、数据库的应用

◇启动 mysql 数据库的服务

(1) 、 windowXp 下 C:>mysqld 回车即可。(这里将 mysql 的 bin 路径加入到环境变量的 path 中,使其各种路径下都能直接启动)

(2) 、 linux 下 #service mysqld start 回车即可。

◇登录数据库

首先要确认服务器处于运行状态。 ( 默认的超级用户名为: root )

C:> mysql –h hostname -u username –p

Enter password:******

hostname 为 mysql 的服务器名或服务器的 IP

username 为用户名

***** 为用户密码

◇ 本地测试:

>mysql -h localhost -u root -p

Enter password: ******

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 9 to server version: 4. 1.10a -nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>


◇ 显示当前用户可操作的数据库:用 show 命令

mysql> show databases;

+----------+

| Database |

+----------+

| mysql |

| test |

+----------+

2 rows in set (0.00 sec)


◇ 确定使用其中的一个数据库:用 use 命令

mysql> use mysql;

Database changed


◇ 显示当前数据库中的所有

mysql> show tables;

+----------------------------------------+

| Tables_in_mysql |

+----------------------------------------+

| columns_priv |

| db |

…….

| time_zone_transition_type |

| user |

+-----------------------------------------+

15 rows in set (0.00 sec)


◇ 建立数据库(建数据库之前,必须确认当前不存在该数据库,否则用 drop 命令删除掉)

mysql> drop database if exists mydb;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create database mydb;

Query OK, 1 row affected (0.02 sec)


◇ 建立表 user

mysql> use mydb;

Database changed

mysql> drop table if exists user;

Query OK, 0 rows affected (0.00 sec)

mysql> create table user (

-> ID int(5) NOT NULL auto_increment,

-> Name varchar(20) NOT NULL default '',

-> Password varchar(20) NOT NULL default '',

-> PRIMARY KEY (ID)

-> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Query OK, 0 rows affected (0.06 sec)


◇ 显示 user 表的结构

mysql> desc user;

+----------+-------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+----------------+

| ID | int(5) | | PRI | NULL | auto_increment |

| Name | varchar(20) | | | | |

| Password | varchar(20) | | | | |

+----------+-------------+------+-----+---------+----------------+

3 rows in set (0.01 sec)


◇ 数据库的导出:用自带的 mysqldump 小程序

C:>mysqldump mydb -h localhost -u root -p>mydb.sql

Enter password: ******


说明: mydb 为当前要导出的数据库名称; mydb.sql 数据导出的存放文件

mydb.sql 内容如下:

-- MySQL dump 10.9

--

-- Host: localhost Database: mydb

-- ------------------------------------------------------

-- Server version 4. 1.10a -nt

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

--

-- Table structure for table user

--

DROP TABLE IF EXISTS user;

CREATE TABLE user (

ID int(5) NOT NULL auto_increment,

Name varchar(20) NOT NULL default '',

Password varchar(20) NOT NULL default '',

PRIMARY KEY (ID)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--

-- Dumping data for table user

--

/*!40000 ALTER TABLE user DISABLE KEYS */;

LOCK TABLES user WRITE;

UNLOCK TABLES;

/*!40000 ALTER TABLE user ENABLE KEYS */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


◇ 数据库的导入:

C:>mysql -h localhost -u root -p mydb

 1<mydb.sql ******="" ---="" bin="" enter="" ip="" localhost="" mydb="" mysql="" mysql.sql="" mysqladmin="" password:="" ◇="" 下:="" 为="" 为将要导入的数据文件;="" 为数据将要导入的目标数据库;="" 修改="" 再进入="" 安装目录的="" 方法一:使用="" 用户密码方法:="" 的服务器名或="" 自带的一个小应用程序)="" 说明:="" 首先进入命令行,="" (="" ;="">mysqladmin –h hostname –u username –p password new_password 
 2
 3Enter password: ****** 
 4
 5说明:  localhost  为  mysql  服务器的  host  ,也可以是  mysql  服务器的  IP  。 
 6
 7username  为当前要修改密码的用户名。 
 8
 9new_password  为新密码  (  该字符串不需要加引号  )  。 
10
11******  为原来的密码。 
12
13方法二:使用  SET PASSWORD  语句 
14
15此时,首先需要用超级用户登录。 
16
17mysql&gt; SET PASSWORD FOR username@hostname=PASSWORD(‘ 
18
19new_password’); 
20
21◇ 添加  mysql  用户: 
22
23Mysql&gt;GRANT ALL ON  *.*  username@hostname IDENTIFIED BY 
24
25‘user_password’; 
26
27◇使用  MySql Control Center  来管理数据库,该工具对数据库建立和维护提供友好的可视化操作界面。 
28
29http://dev.mysql.com/downloads/other/mysqlcc.html</mydb.sql>
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus