在mysql配置文件的所有修改都需要保存退出后重启服务
把mysql路径加入环境变量
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile 刷新
mysql忘记密码解决办法
vim /etc/my.cof 打开配置文件
在[mysqld]模块里添加
skip-grant 跳过授权
保存退出
/etc/init.d/mysqld restart 重启mysql服务
mysql -uroot 不需要密码直接进入mysql
mysql> show databases; 查看mysql里有哪些库
mysql> use mysql; 选择mysql 库
Database changed
mysql> show tables; 查看mysql库里有哪些表
mysql> update user set password=password('123456') where user='root'; 修改root密码
修改完成后回到配置文件 把新加的skip-grant行删除
重启mysql服务
完成修改
配置文件的一些实用修改
skip-name-resolve禁止掉DNS的查询,会提高响应速度。注意修改后不要用localhost,要用127.0.0.1
skip-innodb 禁止掉innodb的引擎 ,加上这条以后就不会生成innodb相关的数据文件
innodb和myisam 后者比较小巧
配置慢查询日志 很重要
log_slow_queries = /tmp/mysql.slow (最好是绝对路径)
long_query_time = 1 超时时间
show create table 表名\G 查看建表语句
家目录下.mysql_history 命令历史(隐藏文件)
怎么让系统不记录mysql历史命令
一:
rm -f .mysql_history 删除原文件
ln -s /dev/null .mysql_history 软链接到一个无底洞就没法记录了
二:
修改.mysql_history的读写权限
mysql常用操作
查看都有哪些库 show databases;查看某个库的表 use db; show tables;
查看表的字段 desc tb;
查看建表语句 show create table tb;
当前是哪个用户 select user();
当前库 select database();
创建库 create database db1;
创建表 create table t1 (`id` int(4), `name` char(40));
查看数据库版本 select version();
查看mysql状态 show status;
修改mysql参数 show variables like 'max_connect%'; set global max_connect_errors =1000; (临时更改,想永久生效需修改配置文件 my.cnf)
查看mysql队列 show processlist;
创建普通用户并授权 grant all on *.* to user1 identified by '123456';
grant all on db1.* to 'user2'@'10.0.2.100' identified by '111222'; 针对特定host地址
grant all on db1.* to 'user3'@'%' identified by '231222';%代表所有主机
更改密码 UPDATE mysql.user SET password=PASSWORD("newpwd") WHEREuser='username' ;
查询 select count(*) from mysql.user; select * from mysql.db; select * from mysql.db where host like '10.0.%';
插入 update db1.t1 set name='aaa' where id=1;
清空表 truncate table db1.t1;
删除表 drop table db1.t1;
删除数据库 drop database db1;
修复表 repair table tb1 [use frm];
7. mysql备份与恢复
备份 mysqldump -uroot -p db >1.sql
恢复 mysql -uroot -p db <1.sql
只备份一个表 mysqldump -uroot -p db tb1 > 2.sql(备份表的话恢复时不需要加表名)
备份时指定字符集 mysqldump -uroot -p --default-character-set=utf8 db >1.sql
恢复也指定字符集 mysql -uroot -p --default-character-set=utf8 db < 1.sql