MySQLday01_MySQL基础-Redhat Enterprise 6.4
知识重点:
一:数据库服务概述
二:构建MySQL服务器
三:构建MySQL服务器
四:MySQL数据类型
五:扩展
一:数据库服务概述
DB: DataBase
数据库:依照某中数据模型并存放到存储器(传说中的硬盘)的数据集合
DBA:DataBaseAdministrator
数据库管理员:
DBMS: DataBase ManagementSystem
数据库管理系统:用来操纵和管理数据库的大型服务软件
DBS:DataBase System
数据库系统:即DB+DBMS,指带有数据库并整合了数据库管理软件的计算机系统
二:构建MySQL服务器
rhel6OS
用ip地址是192.168.1.100主机座数据库服务器
1,提供数据库服务的软件有哪些?
微软:
access 不跨平台,商业(不开源) sql server 不跨平台,商业(不开源) |
oracle公司:(sun把mysql收购,oracle把sun收购了!)
mysql 跨平台,开源 oracle 跨平台,商业(不开源) |
IBM公司:
DB2 跨平台,商业(不开源) |
Sybase公司:
Sybase 跨平台,商业(不开源) |
伯克利大学:
PostgreSQL 跨平台,开源 |
2,mysql主要特点
优点:
-开源 -跨平台 -适用于小规模、关系型数据库系统 -支持Linux/Uinx、Windwos等多种操作系统 -使用C和C++编写,可移植性强 -通过API支持Python/Java/Perl/PHP等语言 |
典型应用环境:
-LAMP平台,与Apache HTTP Server组合 -LNMP平台,与Nginx组合 |
三:MySQL数据库基本管理
1,构建MySQL系统
# tar xvf MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar MySQL-shared-5.6.15-1.el6.x86_64.rpm //共享库 MySQL-devel-5.6.15-1.el6.x86_64.rpm //库和头文件 MySQL-embedded-5.6.15-1.el6.x86_64.rpm //嵌入式版本 MySQL-test-5.6.15-1.el6.x86_64.rpm //测试包 MySQL-server-5.6.15-1.el6.x86_64.rpm //主程序(服务器安装) MySQL-shared-compat-5.6.15-1.el6.x86_64.rpm //兼容包 MySQL-client-5.6.15-1.el6.x86_64.rpm //测试包(客户端安装) |
2,安装mysql软件
[root@localhost 桌面]# rpm -Uvh MySQL-*.rpm |
3,为服务器设置固定ip地址
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes BOOTPROTO=none HWADDR=00:0C:29:BA:CC:2E IPADDR=192.168.1.100 NETMASK=255.255.255.0 [root@localhost ~]# service network restart |
4,启动mysql服务
mysql5.6版本的的服务名为mysql,其他低版本的为mysqld
[root@localhost ~]# service mysql start Starting MySQL.. [确定] [root@localhost ~]# netstat -anptu | grep mysql tcp 0 0 :::3306 :::* LISTEN 5034/mysqld [root@localhost ~]# |
5,mysql服务相关信息
进程名 mysql 监听端口 3306 传输协议 TCP 进程所有者 mysql 数据库存储目录 /var/lib/mysql/ PID文件 /var/lib/mysql/localhost.localdomain.pid 错误日志 /var/log/mysqld.log /var/lib/mysql/mysql.sock 只有数据库启动了才有,停止就没了! 如果访问服务器的时候,找不到mysql.sock,那就说明没开启这个服务! |
[root@localhost ~]# grep --color mysql /etc/passwd mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash [root@localhost ~]# |
6,访问数据库服务器
注:默认情况下只允许数据库管理员从数据库服务器本机登录
格式:mysql -u用户名 -p密码 -h服务器ip地址/或主机名 (注:选项后无空格)
初始状态下,在安装mysql软件的时候,会随机生成密码,存放在管理员的家目录下:隐藏文件.mysql_secret
[root@localhost ~]# cat .mysql_secret # The random password set for the root user at Thu Mar 13 10:28:12 2014 (local time): 8INvuloC [root@localhost ~]# mysql -uroot -hlocalhost -p Enter password: //这里输入密码 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.15 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> quit //退出 Bye [root@localhost ~]# |
7,重置数据库管理员的密码
注:默认状态下,使用安装mysql时自动生成的密码,但是进去之后是不能进行任何操作的,必须先为数据库管理员修改密码。
mysql中不区分大小写
修改密码:set password for 'root'@'localhost'=password(‘123456’);
[root@localhost ~]# mysql -uroot -hlocalhost -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.15 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> select user(); //直接执行的话,会提示强制管理员修改密码 ERROR 1820 (HY000): You must SET PASSWORD before executing this statement mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('123456'); Query OK, 0 rows affected (0.08 sec) mysql> quit Bye [root@localhost ~]# |
8,登录数据库,查看相关信息
selectuser(); //查看当前用户
selectdatabase(); //查看当前工作的库
showdatabases; //查看有哪些库
createdatabase xxx; //新建库
usexxx; //切换数据库
建库的规则:
唯一性:不能有同名的数据库,所以建库之前需要用show databases查看; 区分大小写; 不能纯数字;可以以数字开头,但是不能用纯数字 不能用关键字; 不能用特殊符号:*、(、)、!、^等 |
数据库服务器的库和表都是以文件的方式存放的!
注:删除/var/lib/mysql里面对应的库,那么在mysql里面使用show databases查看对应的库,也就没有了!
mysql> select user(); //查看当前用户 +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.02 sec) mysql> select database();//查看当前所在的库,null表示没有进入库 +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.05 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | //存储数据库服务器上已有的库和表的信息 | mysql | //授权库(非常重要)用户,密码存放在该库 | performance_schema | //当前数据库服务器运行时的参数信息 | test | //公共库 +--------------------+ 4 rows in set (0.08 sec) mysql> create database student; //新建一个库student Query OK, 1 row affected (0.05 sec) mysql> use student //切换库 Database changed mysql> select database(); //查看当前使用的库 +------------+ | database() | +------------+ | student | +------------+ 1 row in set (0.00 sec) mysql> drop database student; //删除数据库student Query OK, 0 rows affected (0.18 sec) mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> quit Bye [root@localhost ~]# |
示例:创建一个列表stu_tab
mysql>create db1;
mysql>use db1;
mysql>create table stu_tab(age int);
mysql>desc stu_tab; //查看stu_tab表的表结构
mysql>insert into stu_tab(age)values(19);
mysql>insert into stu_tab(age)values(19),(30),(40);
示例:创建一个列表stu2
mysql> create table stu2(name char(3),age int); //新建一个列表 mysql> insert into stu2(name,age)value("jim",23)("tom",22); mysql> desc stu2; //查看stu2的表结构 mysql> select * from stu2; //查看stu2表的信息 mysql> show tables; mysql> |
四:MySQL数据类型
数值类型:体重、身高、成绩、工资
字符类型:姓名、工作单位、通讯地址
日期时间类型:出生日期、注册日期
枚举类型:兴趣爱好、性别
1,数值类型:
整型:小整数、大整数、极大整数
tinyintunsigned:无符号tinyint类型(范围为0-255)
mysql>create table stu01(name char(3),age tinyint unsigned);
2,字符类型
char :定长
特点:不够的话,用空格补起,超过了就存不上
varchar :变长
特点:更具用户存储数据的多少来开辟存储空间,超过了也存不上!
注:数值类型的宽度,是显示宽度;
int(3)表示显示宽度为3,并不是限制存储的大小。
如果输入的值为1,那么将会补两个空格,如果是11,则补一个空格,如果超过3个宽度,则完全显示。默认的宽度为11。
3,日期时间类型:生日、注册时间、入职时间、入学时间
年 year
日期 date
时间 time
日期时间 timestamp/datetime
函数示例:
mysql> select now(),sysdate(),curdate(),curtime(); +---------------------+---------------------+------------+-----------+ | now() | sysdate() | curdate() | curtime() | +---------------------+---------------------+------------+-----------+ | 2014-03-13 16:06:34 | 2014-03-13 16:06:34 | 2014-03-13 | 16:06:34 | +---------------------+---------------------+------------+-----------+ 1 row in set (0.00 sec) mysql> select year(now()),month(now()),time(now()); +-------------+--------------+-------------+ | year(now()) | month(now()) | time(now()) | +-------------+--------------+-------------+ | 2014 | 3 | 16:05:21 | +-------------+--------------+-------------+ 1 row in set (0.00 sec) mysql> select sleep(5); //五秒后显示 +----------+ | sleep(5) | +----------+ | 0 | +----------+ 1 row in set (5.06 sec) mysql> |
year年份的处理:
默认用4位数字表示;
当只用2位数字赋值时,01-69视为2001-2069,70-99视为1970-1999
00视为0000;
datetime和timestamp日期时间:
当未给timestamp字段赋值时,自动以当前时间赋值,而datetime字段默认赋值为0。
4,枚举类型:用于性别、爱好等
字段的值要在列举的范围内选择
set(值1,值2,...) 多选可以用编号1,2,...
enum(值1,值2,...) 单选不可以用编号
扩展:修改/etc/my.cnf文件,添加字段
1,调整字符集设置,
重启mysql服务,可以使mysql支持utf8字符:
[root@localhost ~]# vim /etc/my.cnf [root@localhost ~]# cat /etc/my.cnf | grep char #character_set_server=utf8 //不添加这一行 [root@localhost ~]# service mysql restart Shutting down MySQL.. [确定] Starting MySQL.. [确定] [root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.15 MySQL Community Server (GPL) 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> show variables like 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) mysql> exit Bye [root@localhost ~]# vim /etc/my.cnf [root@localhost ~]# cat /etc/my.cnf | grep char character_set_server=utf8 [root@localhost ~]# service mysql restart Shutting down MySQL.. [确定] Starting MySQL. [确定] [root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.15 MySQL Community Server (GPL) 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> show variables like 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection| utf8 | | character_set_database | utf8 | | character_set_filesystem| binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.01 sec) mysql> exit Bye [root@localhost ~]# |
2,查看一个table是如何建立的:
showcreate table 表名
mysql> show create table student; +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` ( `学号` char(9) NOT NULL, `姓名` varchar(4) NOT NULL, `性别` enum('男','女') NOT NULL, `手机号` char(11) DEFAULT '', `通讯地址` varchar(64) DEFAULT NULL, PRIMARY KEY (`学号`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> |
作业:
create table stu_tab(
学号 char(10) not null,
姓名 char(3) not null,
性别 enum('男','女') notnull,
年龄 tinyint unsigned not null,
出生日期 date not null,
薪资 float(7,2) default '0.00',
手机号 char(11) default '',
家庭住址 char(64) not null
)default charset=utf8;
insert into stu_tabvalues("NSD1312001","zsp",1,23,19910520,15000,13100000001,"河南省南阳市");
insert into stu_tabvalues("NSD1312002","gbw",1,22,19901212,8500,13100002222,"河北省邯郸市");
insert into stu_tabvalues("NSD1312003","xjx",1,28,19881122,10000,13100005558,"北京市朝阳区");
insert into stu_tabvalues("NSD1312004","jyp",1,24,19900325,18000,13100444444,"河南省许昌市");
示例:
mysql> create table stu_tab( -> 学号 char(10) not null, -> 姓名 char(3) not null, -> 性别 enum('男','女') not null, -> 年龄 tinyint unsigned not null, -> 出生日期 date not null, -> 薪资 float(7,2) default '0.00', -> 手机号 char(11) default '', -> 家庭住址 char(64) not null -> )default charset=utf8; Query OK, 0 rows affected (0.03 sec) mysql> insert into stu_tab values("NSD1312001","zsp",1,23,15000,13100000001,"河南省南阳市"); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> desc stu_tab; +--------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------------------+------+-----+---------+-------+ | 学号 | char(10) | NO | | NULL | | | 姓名 | char(3) | NO | | NULL | | | 性别 | enum('男','女') | NO | | NULL | | | 年龄 | tinyint(3) unsigned | NO | | NULL | | | 出生日期 | date | NO | | NULL | | | 薪资 | float(7,2) | YES | | 0.00 | | | 手机号 | char(11) | YES | | | | | 家庭住址 | char(64) | NO | | NULL | | +--------------+---------------------+------+-----+---------+-------+ 8 rows in set (0.00 sec) mysql> insert into stu_tab values("NSD1312001","zsp",1,23,19910520,15000,13100000001,"河南省南阳市"); Query OK, 1 row affected (0.00 sec) mysql> insert into stu_tab values("NSD1312002","gbw",1,22,19901212,8500,13100002222,"河北省邯郸市"); Query OK, 1 row affected (0.00 sec) mysql> insert into stu_tab values("NSD1312003","xjx",1,28,19881122,10000,13100005558,"北京市朝阳区"); Query OK, 1 row affected (0.00 sec) mysql> insert into stu_tab values("NSD1312004","jyp",1,24,19900325,18000,13100444444,"河南省许昌市"); Query OK, 1 row affected (0.00 sec) mysql> select * from stu_tab; +------------+--------+--------+--------+--------------+----------+-------------+--------------------+ | 学号 | 姓名 | 性别 | 年龄 | 出生日期 | 薪资 | 手机号 | 家庭住址 | +------------+--------+--------+--------+--------------+----------+-------------+--------------------+ | NSD1312001 | zsp | 男 | 23 | 1991-05-20 | 15000.00 | 13100000001 | 河南省南阳市 | | NSD1312002 | gbw | 男 | 22 | 1990-12-12 | 8500.00 | 13100002222 | 河北省邯郸市 | | NSD1312003 | xjx | 男 | 28 | 1988-11-22 | 10000.00 | 13100005558 | 北京市朝阳区 | | NSD1312004 | jyp | 男 | 24 | 1990-03-25 | 18000.00 | 13100444444 | 河南省许昌市 | +------------+--------+--------+--------+--------------+----------+-------------+--------------------+ 4 rows in set (0.00 sec) mysql> |
本文转自 murongqingqqq 51CTO博客,原文链接:http://blog.51cto.com/murongqingqqq/1376190