1. /usr/local/mysql/bin/mysqld_safe & (启动)
2. lsof -i :3306
3. mysql -uroot -p‘chengye123‘ 单实例登录
4. mysql -uroot -p‘chengye123‘ -S /data/3306/mysql.sock (多实例登录)
5. ;or \g 结束mysql语句 \h 查看帮助 不区分大小写
6. show databases; 查看数据库
7. mysqladmin -uroot password ‘211570xcy‘ 单实例设置密码
8. mysqladmin -uroot password ‘211570xcy’ -S /data/3306/mysql.sock 多实例设置密码
9. 修改密码 mysqladmin -uroot -p‘211570xcy‘ password ‘chengye123‘
10. mysqladmin -uroot -p’chengye123’ password ‘211570xcy’ -S /data/3306/mysql.sock
11. Update mysql.user set password=password("211570xcy") where user=‘root‘ and host="localhost";
flush privileges; 在mysql中修改密码
12. set password=password(‘chengye123‘);
flush privileges;
13. 找回丢失的mysql 密码
killall mysqld
mysqld_safe --skip-grant-tables & 在后台无密码启动
mysql
Update mysql.user set password=password("211570xcy") where user=‘root‘ and host="localhost";
flush privileges;
14. 多实例
killall mysqld
mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables &
Update mysql.user set password=password("211570xcy") where user=‘root‘ and host="localhost";
flush privileges;
15. SQL结构化查询(6个部分)
数据查询语言(DQL)----select
数据操作语言(DML)---Insert/update/delete
事物处理语言(TPL)----Transaction/commit/roolback
数据控制语言(DCL)----grant/revoke
数据定义语言(DDL)---create/drop
指针控制语言(CCL)----declare cursor/fetch into/update where current
16. create database chengye; 创建数据库
17. show databases like ‘chengye%‘; 查看
18. show create database chengye; 查看建表语句
- 19. create database chengye_gbk default character set gbk collate gbk_chinese_ci; 建一个名为chengye_gbk的GBK字符集数据库
字符集 校对规则
20. create database chengye_utf8 character set utf8 collate utf8_general_ci; 建一个名为chengye_utf8的utf8字符集数据库
字符集是用来定义mysql字符串的存储方式;mysql字符集包括字符集(character)和校对规则(collation)
21. drop database if exists chengye; 删除数据库
22. select user(); 查看当前用户 select database();连接数据库
use chengye_gbk; 进入库中 select version(); 查看mysql版本
show tables from chengye_gbk;查看库中的表
drop user ‘‘@‘localhost‘; 删除多余系统账号、
select user,host from mysql.user; 查看
delete from mysql.user where user=‘root‘ and host=‘localhost‘;
23. 创建用户,再进行授权
create user chengye@‘localhost‘ identified by ‘chengye123‘;
select user from mysql.user; 查看用户
show grants for chengye@‘localhost‘; 查看用户权限,默认USAGE
grant all on chengye_gbk.* to chengye@‘localhost‘; 把库的所有权限赋予 flush privileges; show grants for chengye@‘localhost‘;
24. grant all on chengye_utf8.* to chengye@‘localhost‘ identified by ‘211570xcy‘;直接授权
25. 远程连接另外一台的数据库失败
26. revoke insert on chengye_gbk.* from chengye@‘localhost‘; 收回权限
27. 生产环境授权:grant select,insert,update,delete,create,drop on chengye_gbk.* to chengye@‘192.168.1.%‘ identified by ‘211570xcy‘;
28. 例:create database blog default character set gbk collate gbk_chinese_ci;
use blog select database();
建表 create table 表明(
字段名 类型(长度) [约束],
字段名 类型(长度) [约束]
);
create table test(
-> uid int(32) primary key auto_increment,
-> uname varchar(32)
-> );
show tables from blog;
show create table test\g
Mysql-5.1 默认ENGINE=MYISAM DEFAULT CHARSET=gbk 引擎
Mysql-5.5 默认ENGINE=InnoDB DEFAULT CHARSET=gbk
mysql 字段类型:1. INT(正常大小整数类型) 2. DOUBLE(正常大小浮点数类型) 3.DATE(日期类型) 4. CHAR(定长字符串类型) 5. BLOB TEXT
6. VARCHAR型(变长字符串类型)
29. 建立Student表: create table student(
-> Sno int(10) NOT NULL COMMENT ‘xuehao‘,
-> Sname Varchar(16) NOT NULL COMMENT ‘xingming‘,
-> Ssex char(2) NOT NULL COMMENT ‘xingbie‘,
-> Sage tinyint(2) NOT NULL default ‘0‘ COMMENT ‘nianling‘,
-> Sdept varchar(16) default NULL COMMENT ‘xibie‘,
-> PRIMARY KEY (Sno),
-> KEY index_Sname (Sname)
-> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
30. show tables; desc student;查看表结构 drop table test; 删除表
31. 每个表只有一个主键,但是可以有多个索引,主键要求列的唯一性,索引不要求唯一
32. 建表时创建索引create table test(
-> id int(4) not null primary key auto_increment, 常主键 自增
-> name char(20) not null,
-> KEY `index_name` (`name`) 索引 索引名称内容
-> );
show index from test; desc test;
33. alter table test drop index index_name; 删除索引
show index from test;
34. 建表后添加索引 alter table test add index INDEX_name(name);
create INDEX INDEX_name on test(name); 不能创建主键索引
35. 删除主键:非自增列 alter table test drop PRIMARY KEY;
若有自增列不能删,建表时去掉 PRIMARY KEY auto_increment
36. 表操作:索引可以加快查询速度,若建索引多,更新数据库还要维护,尽量选择在唯一值多的大表上建索引,小表上不要建索引
use chengye_gbk
create table test(
-> id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> KEY `index_name` (`name`)
-> );
insert into test(id,name) values(001,‘chengye‘); 插入数据
select * from test; 查看数据
insert into test values (4,‘evan‘),(5,‘jiangfang‘); 批量插入
select * from test limit 2; 查询两行
select * from test order by id limit 0,3; 排序输出
select * from test where id=4; 指定ID
select * from test where name=‘jiangfang‘;
select * from test where id>3 and id<5; 选择性输出
37. use blog
set names gbk;
insert into student values(0001,‘chengye‘,‘nian‘,24,‘jisuanji‘);
select * from student;
select Sname,sum(grade) from sc a,student b where a.Sno=b.Sno group by Sname; 查看每个学生所修学分的总和
explain select * from test where id=6\g 查看有无走索引
update test set name=‘gaigai‘ where id=1; 修改数据
mysqldump -uroot -p’211570xcy’ -B chengye_gbk >/mnt/tmp.sql 备份
egrep -v “\-|\*|^$” /mnt/tmp.sql
mysql -uroot -p’211570xcy’ </mnt/tmp.sql 还原数据
update test set id=100 where name=‘evan‘;
delete from test where id=100; 删除表中数据
truncate table test; 清空表
38. 在表中增删改desc test;
alter table test add sex char(4); 添加性别列
alter table test add age int(4) after name; 指定位置加入
39. 更改表名rename table test to chengye;
alter table chengye rename to test;
drop table test; 删除表