0521MySQL经常使用操做---设置更改root密码、数据库备份恢复、链接mysql、mysql用户管理

 

13.1 设置更改root密码
13.2 链接mysql
13.3 mysql经常使用命令
13.4 mysql用户管理
13.5 经常使用sql语句
13.6 mysql数据库备份恢复
使用xtrabackup备份innodb引擎的数据库  innobackupex 备份 Xtrabackup 增量备份 http://zhangguangzhi.top/2017/08/23/innobackex%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql
相关视频  
连接:http://pan.baidu.com/s/1miFpS9M 密码:86dx  
连接:http://pan.baidu.com/s/1o7GXBBW 密码:ue2f

扩展
mysql5.7 root密码更改   http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎对比  http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置详解: http://blog.linuxeye.com/379.html
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同窗分享的亲身mysql调优经历:  http://www.apelearn.com/bbs/thread-11281-1-1.html
SQL语句教程  http://www.runoob.com/sql/sql-tutorial.html
什么是事务?事务的特性有哪些?  http://blog.csdn.net/yenange/article/details/7556094
根据binlog恢复指定时间段的数据   https://blog.csdn.net/lilongsy/article/details/74726002
mysql字符集调整  http://xjsunjie.blog.51cto.com/999372/1355013html

 

笔记:mysql

mysql特性、趋势http://www.javashuo.com/article/p-zmurcxoy-br.htmllinux

https://downloads.mariadb.org/web

主从:关键词GTID http://www.javashuo.com/article/p-dztgsawh-k.htmlsql

 http://www.javashuo.com/article/p-eexealuq-cq.htmlshell

 http://www.javashuo.com/article/p-oiyqmdti-bc.html数据库

mysql 参数调整http://isky000.com/database/mysql-perfornamce-tuning-cache-parametervim

 

 

1、设置更改root密码服务器

ps aux |grep mysql  看mysql是否启动ide

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服务

2、链接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查看都有什么数据库,引号中的命令能够更改

3、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这里显示比较完整)

4、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;

5、经常使用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;  删除库

7、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
相关文章
相关标签/搜索