13.1 设置更改root密码
13.2 连接MySQL
13.3 MySQL常用命令
13.4 MySQL用户管理
13.5 常用sql语句
13.6 MySQL数据库备份恢复
设置更改root密码
/usr/local/mysql/bin/mysql -uroot
更改环境变量PATH,增加mysql绝对路径 // export PATH=$PATH:/usr/local/mysql/bin/并加入到/etc/profile
source /etc/profile //使变量生效,这样mysql -uroot 就能进入mysql。
mysqladmin -uroot password '123456' // 把root的密码改为123456
mysql -uroot -p123456
如果不知道root的密码,就要进行密码重置如下操作:
vi /etc/my.cnf//在mysql路径定义那里增加skip-grant
重启mysql服务 /etc/init.d/mysqld restart
mysql -uroot
use mysql;
update user set password=password('123456') where user='root';
在充值root密码之后我们要把之前/etc/my.cnf/中增加的skip-grant删除,若不删除那所有的用户就都不需要加密码就能登录,非常不安全,然后重新使用之前设置的 密码就能进入mysql
mysql -uroot -p123456
13.3 MySQL常用命令
创建库 create database db1;
创建表 use db1; create table t1(`id` int(4), `name` char(40));
查看当前数据库版本 select version();
查看数据库状态 show status;
查看各参数 show variables; show variables like 'max_connect%';
修改参数 set global max_connect_errors=1000;
查看队列 show processlist; show full processlist;
MySQL创建用户以及授权
grant all on *.* to 'user1' identified by 'passwd';
grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd';
grant all on db1.* to 'user3'@'%' identified by 'passwd';
show grants;
show grants for user2@192.168.133.1;
常用SQL语句
select count(*) from mysql.user;
select * from mysql.db;
select db from mysql.db;
select db,user from mysql.db;
select * from mysql.db where host like '192.168.%';
insert into db1.t1 values (1, 'abc');
update db1.t1 set name='aaa' where id=1;
truncate table db1.t1; //清空表的内容
drop table db1.t1;
drop database db1;
MySQL数据库的备份与恢复
备份库 mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
恢复库 mysql -uroot -p123456 mysql < /tmp/mysql.sql
备份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql
恢复表 mysql -uroot -p123456 mysql < /tmp/user.sql
备份所有库 mysqldump -uroot -p -A >/tmp/123.sql
只备份表结构 mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql