MySQL
SQL (Structured Query Language)
n. sql, 结构化谘询语言, 定义寻找的资料库语言 (计算机用语)
Database
数据库:
层次
网状
关系
对象 -- 关系
非关系
常见数据库软件:
Oracle, Sybase, Informix, DB2, EnterpriseDB
SQL Server, Access
MySQL, PostgreSQL
Memorydb内存数据库:mysql, sqlite, altibase, timestin
NoSQL:key-value 键值对,扩展性好,适合大型
MySQL特性:
speed 速度
reliability 可靠
scalability 可扩展
ease of use 易用
protability and standards compliance 可移植性,兼容性
multiuser support 支持多用户
internationalization 支持国际化
wide application support 广泛应用程序
open-source code 开源
product family:
MySQL server
MySQL cluster
255 nodes
MySQL proxy 代理
MySQL administrator
MySQL query browser
MySQL workbench
MySQL migration toolkit
MySQL embedded server
MySQL drivers and connectors
安装方式:
rpm (redhat,mysql)
binary format 绿色
source code 源码
按以下顺序查找配置文件:
/etc/my.cnf
/etc/mysql/my.cnf
$MySQL_HOME/my.cnf
/path/to/file when defaults-extra-file=/path/to/file is specified
~/.my.cnf
datadir 指定data目录
让mysqld启动读取额外的配置文件
username@hostname, password
root,192.168.0.1
五个默认账号:
root,127.0.0.1
root,localhost
root,hostname
localhost
hostname
mysql
-u, --user=
-h, --host=
-p, --password=
--protocol={tcp|socket|pipe|memory}
--port=
修改密码:
mysqladmin -u root password'new_password'
mysqladmin -u root -h this_host_name password'new_password'
mysql>SET PASSWORD FOR 'root'@'host_name'=PASSWORD('new_password');
mysql>UPDATE user SET password=PASSWORD('redhat') WHERE user='root' and host='server.example.com';
mysql>FLUSH PRIVILEGES; 刷新系统权限表
创建用户:
create user 'root'@'%.example.com' identified by '123456';
收回权限:
revoke insert on *.* from cacti@'%';
显示授于用户的权限:
show grants for cacti@'%';
显示当前用户:
select user();
select current_user();
删除用户:
DROP USER 'root'@'station.example.com';
DELETE FROM user where user='';
mysql提示符:
>
->
'>
">
`>
/*> /* */ 成对出现
ctrl+w/y/u/d/e
命令补全:\#, rehash
取消命令执行:
ctrl+u
\c 前不能有;
\d 设置分隔符 如:\d //
\g go
\G 竖向显示
\! ls /etc 交互式使用shell命令
\P more; 分页
#vim test.sql
user mysql;
select host,user,password from mysql.user;
#mysql < test.sql
#mysql -e 'select host,user,password from mysql.user'
>SOURCE /root/test.sql
help STATUS 显示帮助
--safe-updates
忘记密码:
/usr/local/mysql/bin/mysqld --user=mysql --skip-grant-tables &
user mysql
UPDATE user SET password=PASSWORD('123456') where user='root' and host='localhost';
FLUSH PRIVILEGES;
gui Tools:
SQLyog
MySQL Front
phpMyAdmin
MySQL Query Brower
MySQL Administrator
MySQL workbench
数据库设计的范式
约束
侯选键, key
primary key 主键
foreign key 外键
unique key 与主键区别允许为空
check
SQL:
DDL:数据定义语言
DML:数据操作语言
DCL:数据库控制语言
创建表:
create table books (
id INT,name VARCHAR (255),author VARCHAR(255));
show create table t1\G; 显示创建表使用的命令
显示表的结构
DESCRIBE/DESC books;
修改为无符号的整型,不为空并且为主键:
ALTER TABLE books CHANGE id id INT UNSIGNED NOT NULL PRIMARY KEY;
ALTER TABLE books CHANGE id id INT UNSIGNED NOT NULL AUTO_INCREMENT; 自动加1
ALTER TABLE tb_name MODIFY filed data_type
新增加字段:
ALTER TABLE tb_name ADD new_filed data_type
ALTER TABLE books ADD intime DATE NOT NULL DEFAULT '2011-2-26';
在某一字段后面添加字段:
ALTER TABLE books ADD pubtime DATE NOT NULL AFTER nmae;
删除字段:
ALTER TABLE books DROP puttime;
给表重命名:
ALTER TABLE books RENAME books2;
RENAME TABLE books2 TO books;
修改表引擎:
ALTER TABLE books ENGINE=innodb;
SHOW ENGINES;显示支持的引擎种类
SHOW TABLE STATUS;显示当前表的引擎
BLACKHOLE 黑洞
CSV 纯文本存储数据
删除表/数据库:
DROP TABLE tb_name
DROP DATABASE db_name
清空表并重置计数器:
TRUNCATE TABLE tb_name;
mysqldump -uroot -p --database db_name > /root/dbname.sql
查看Mysql表占用大小:
select table_name,DATA_LENGTH/1024/1024 as 'data_size(MB)' from tables where table_schema='test';
/opt/app/mysql5/bin/mysql -uroot -p'mysql' -se "show table status from test;" | awk 'NR>=2{data_size+=$7}END{print "data_size\t"(data_size/1024/1024/1024)"G"}'