13.1 设置更改root密码
13.2 连接mysql
13.3 mysql常用命令
13.4 mysql用户管理
13.5 常用sql语句
13.6 mysql数据库备份恢复
使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份
相关视频
扩展
mysql5.7 root密码更改
myisam 和innodb引擎对比
mysql 配置详解
mysql调优
同学分享的亲身mysql调优经历
SQL语句教程
什么是事务?事务的特性有哪些?
根据binlog恢复指定时间段的数据
mysql字符集调整
笔记:
mysql特性、趋势
主从:关键词GTID
mysql 参数调整
一、设置更改root密码
ps aux |grep mysql 看mysql是否启动
ls /usr/local/mysql/bin/mysql 命令所在目录,但是此目录并没有在环境变量中
echo $PATH
1. 把mysql命令加入环境变量PATH,更改环境变量PATH,增加mysql绝对路径
vim /etc/profile 在最后加入:
export PATH=$PATH:/usr/local/mysql/bin/
2. 重启服务/重新加载profile文件
/etc/init.d/mysqld restart source /etc/profile
3. 设置root密码
mysqladmin -uroot password 'tobe' //单引号可加可不加,但有特殊字符最好加上,-u指定用户 mysql -uroot -p //-p选项后面可跟密码( mysql -uroot -pmysql),也可不跟密码,不跟密码是交互式登录
更改密码
如果知道root密码,可以使用mysqladmin直接更改密码
mysqladmin -uroot -p'mysql' password 'tobe'
如果不知道root密码,需要按如下方法重置mysql密码
1.在/etc/my.cnf配置文件里添加 skip-grant,意思是忽略授权
/etc/init.d/mysqld restart 重启服务
2.mysql -uroot 进入到库,不需要使用密码,直接登录 3.use mysql 选择库,密码存在该库中 4.update user set password=password('tobe123') where user='root'; 更改密码 5.编辑/etc/my.cnf,取消skip-grant 6./etc/init.d/mysqld restart 重启mysql服务
二、连接mysql
1. 连接本地的数据库
mysql -uroot -ptobe123
2.远程连接
mysql -uroot -ptobe123 -h127.0.0.1 -P3306 // -P指定端口 -h指定host(IP)
3.通过sock连接
mysql -uroot -ptobe123 -S/tmp/mysql.sock //-S指定sock文件,只适合在本机使用
4.连接mysql后运行命令(多数使用在shell脚本里)
mysql -uroot -ptobe123 -e "show databases" //-e查看都有什么数据库,引号中的命令可以更改
三、mysql常用命令
库是由表组成的。表由字段组成的 库---表--字段
mysql的命令历史记录在.mysql_history
查询库 show databases; 切换库 use mysql; 查看库里的表 show tables; 查看表里的字段 desc tb_name; //tb_name为表名 查看建表语句 show create table tb_name\G; \G表示竖排显示 查看当前用户 select user(); 查看当前使用的数据库 select database();
在命令前加#即可注释,命令不生效
创建库 create database tobe; 创建表 先选择库 use tobe; create table t1(`id` int(4), `name` char(40)); t1为表名,id、name为字段,int为num格式最长4,char字符串格式最长40 create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8; 建表时设置字符集为utf8 删除表 drop table t1;
查看当前数据库版本 select version(); 查看数据库状态 show status; 查看mysql的参数,这些参数也可以再/etc/my.cnf定义 show variables; 也可以指定查找 show variables like 'max_connect%'; %表示通配,当不记得字母可以使用这个
修改mysql参数 set global max_connect_errors=1000; global只是临时修改,要永久生效需要去配置文件添加vim /etc/my.cnf
查看当前mysql服务器的队列 可以查看当前mysql在干什么,也可以发现是否有锁表 show processlist; show full processlist; 完整查看(可以看到info这里显示比较完整)
四、mysql用户管理
创建用户:
grant :授权 ,all:所有的权限 *.* 表示允许操作那些库和表,库(.前面的*是库)和表(.后面的*)用.分开
grant all on *.* to 'user1' identified by 'user1';
grant all on *.* to 'user2'@'127.0.0.1' identified by '123456'; 需要使用-h指定ip才可以连接
grant all on *.* to 'user3'@'localhost' identified by '123456'; localhost针对sock连接,不需要使用-h
创建用户且只能使用SELECT,UPDATE,INSERT命令和操作db1库,@后面指定来源IP
grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'localhost' identified by 'user2';
创建用户使用全部命令和全部库,任何机器都可以连接 %表示全部来源IP
grant all on *.* to 'user2'@'%' identified by 'user2';
查看用户权限;
show grants; 只能查看当前登录用户的权限,复制用户时会用到
查看其它用户权限
show grants for user1;
如果有指定IP的用户需要加上@后面跟指定的IP
show grants for user2@127.0.0.1;
五、常用sql语句
查询语句;
count(*)表示表中有几行 mysql.user :表示查询mysql库中的user表
select count(*) from tobe.t1; 查询tobe库中的t1表有多少行
查询表中的所有数据
tobe.t1:表示查询mysql库中的user表 *表示所有数据
select * from tobe.t1\G;
查询指定字段的数据,可以指定多个字段查询数据
select id,name from tobe.t1;
插入一行数据:
use tobe; 切换到tobe库 insert into t1 values (1,'abc'); 插入一行数据 select * from t1;
更改表中数据
update t1 set id='123' where name='two';
清空表中数据,表中结构保留
truncate table tobe.t1;
desc查看字段,可以看到只清空了表的数据,结构还是保留了
desc tobe.t1;
删除表:
show tables; 查询当前库的表 drop table tobe.t1; 删除表
删除库;
show databases; 查询当前所有库 drop database tt; 删除库
七、mysql数据库备份恢复
备份库 mysqldump -uroot -ptobe tobe > /tmp/tobebal.sql 备份tobe库到tmp目录改名为sql后缀
创建一个新的库tt2,进行恢复
恢复库 mysql -uroot -ptobe tt2 < /tmp/tobebal.sql
tobe 库名 t2表名
备份表 mysqldump -uroot -ptobe tobe t2 > /tmp/t2.sql 恢复表 mysql -uroot -ptobe tobe < /tmp/t2.sql 备份所有库 mysqldump -uroot -ptobe -A > /tmp/123.sql 只备份表结构 mysqldump -uroot -ptobe -d tobe > /tmp/tobe1.sql