6月19日任务
MySQL常用操作
13.1设置更改root密码
1.查看mysql是否启动
# ps aux |grep mysql
如果没有启动手动开启
# /etc/init.d/mysqld start
2.把mysql命令路径加入到环境变量PATH中
# export PATH=$PATH:/usr/local/mysql/bin/
修改配置文件使其永久生效
# vim /etc/profile //在末尾增加一行配置
# source /etc/profile //使配置文件生效
3.登录mysql
# mysql -uroot
# mysql -uroot -p //-p指定用户密码,默认是空的,可以直接回车
退出mysql用 quit
4.设置用户密码
# mysqladmin -uroot password 'aminglinux' //设置用户密码
这时就不能直接登录了
# mysql -uroot -paminglinux //指定密码登录
或者这样登录
5.修改密码
# mysqladmin -uroot -p'aminglinux' password 'aminglinux.1'
用新的密码登录
# mysqladmin -uroot -p'aminglinux' password 'aminglinux.1' //密码的单引号尽量加上,否则某些特殊符号可能不识别
6.忘记密码时,修改mysql密码
修改mysql配置文件
# vim /etc/my.cnf //添加一行skip-grant
:
重启mysql
#/etc/init.d/mysqld restart
这时就可以不用密码登录了
进入mysql库
用户密码授权等存储于user表
查看密码信息
mysql> select password from user where user='root' ; //密码是加密的
设置密码
update user set password=password('aminglinux') where user='root';
退出mysql
修改my.cnf ,去掉skip-grant
# vim /etc/my.cnf
重启mysql
# /etc/init.d/mysqld restart
这样就可以用设置好的密码登录了
13.2连接MySQL
常用的连接
1.# mysql -uroot -paminglinux
2.# mysql -uroot -paminglinux -h127.0.0.1 -P3306 //指定ip和端口
3.# mysql -uroot -paminglinux -S/tmp/mysql.sock //实际上是和1一样的
4.# mysql -uroot -paminglinux -e "show databases" //一般-e使用在shell脚本
13.3MySQL常用命令
查询库
show databases;
切换库
use mysql;
查看库里的表
show tables;
查看表里的字段
desc user;
查看建表语句
show create table user\G; //不加/G显示的内容会比较乱
查看当前的用户
select user();
查看当前使用的数据库
select databases();
这个是NULL
切换到mysql库,查询结果就变为mysql
创建库
mysql> create database db1;
查询一下,会发现增加了db1
创建表
切换到db1库
mysql> create table t1(`id` int(4), `name` char(40)); //创建表
mysql> show create table t1\G; //查看表t1的信息
删除表用 drop table t1
可以在创建表的时候指定引擎和字符集
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
查看当前数据库版本
mysql> select version();
也可以在linux命令行下查看
查看数据库状态
mysql> show status;
查看各参数
mysql> show variables;
mysql> show variables like 'max_connect%';
修改参数
mysql> set global max_connect_errors=1000; //这个只在内存中生效,要想永久生效需要修改/etc/my.cnf
查看队列
mysql> show processlist;
mysql> show full processlist;
小常识:
1.# mysql -uroot -paminglinux -h172.31.208.147 //172是内网ip用内网访问的时候会解析出用户名
2.mysql命令历史可以用上下键,命令历史存储于/root/.mysql_history
#cat .mysql_history