MySQL使用笔记
安装MySQL
检查是否安装mysql
rpm -qa | grep mysql
安装
yum install mysql yum install mysql-server yum install mysql-devel
验证安装
mysqladmin --version
卸载
rpm -e mysql // 普通删除模式 rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
启动及关闭 MySQL
检查是否运行
/etc/rc.d/init.d/mysqld status
启动
service mysqld start
关闭
/etc/init.d/mysqld stop
重启
service mysqld restart
MySQL用户管理
MySQL安装成功后,默认的root用户密码为空,为了安全起见给root设置密码:
mysqladmin -u root password 123 (123为密码,也可以写成:'123'或"123")
登录
mysql -u root -p
添加用户
mysql> create user zx_root IDENTIFIED by 'xxxxx'; //identified by 会将纯文本密码加密作为散列值存储
删除用户
mysql>delete from mysql.user where user ='testuser' ; mysql>flush privileges; (刷新系统权限表)
mysql>drop user newuser; //mysql5之前删除用户时必须先使用revoke 删除用户权限,然后删除用户,mysql5之后drop 命令可以删除用户的同时删除用户的相关权限
设置密码
mysql> set password for testuser =password('xxxxxx');
mysql> update mysql.user set password=password('xxxx') where user='otheruser'
#mysqladmin -u root password "new_password"
忘记密码
找到自己的
my.cnf
文件,通常会在 /etc目录下(如果不在,可以用find / -name my.cnf
命令找一下)然后使用
vim my.cnf
命令编辑该文件(建议先备份),在[mysqld]
下面加上skip-grant-tables
,作用是登陆时跳过登陆认证,然后:wq
保存退出然后用
service mysqld restart
重启mysql,接下来输入如下命令:# mysql -uroot -p mysql> use mysql; mysql> update user set password=password('123') where user='root'; mysql> flush privileges; (刷新系统权限表) 退出mysql
删除
my.cnf
中的skip-grant-tables
,重启mysql,完成。
权限管理
- 查看用户权限
mysql> show grants for testuser;
- 设置用户访问数据库权限
>> grant all privileges on test_db.* to testuser@localhost identified by "123456";
//设置用户testuser,只能访问数据库test_db,其他数据库均不能访问 ;
>> grant all privileges on *.* to testuser@localhost identified by "123456";
//设置用户testuser,可以访问mysql上的所有数据库 ;
>> grant all privileges on test_db.user_infor to testuser@localhost identified by "123456";
//设置用户testuser,只能访问数据库test_db的表user_infor,数据库中的其他表均不能访问 ;
- 设置用户操作权限
>> grant all privileges on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION;
//设置用户testuser,拥有所有的操作权限,也就是管理员 ;
>> grant select on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION;
//设置用户testuser,只拥有【查询】操作权限 ;
>> grant select,insert on *.* to testuser@localhost identified by "123456";
//设置用户testuser,只拥有【查询\插入】操作权限 ;
>> grant select,insert,update,delete on *.* to testuser@localhost identified by "123456";
//设置用户testuser,只拥有【查询\插入】操作权限 ;
>> REVOKE select,insert ON what FROM testuser
//取消用户testuser的【查询\插入】操作权限 ;
- 设置用户远程访问权限
>> grant all privileges on *.* to testuser@"192.168.1.100" identified by "123456" ;
//设置用户testuser,只能在客户端IP为192.168.1.100上才能远程访问mysql
- 关于root用户的访问设置
设置所有用户可以远程访问mysql,修改my.cnf配置文件,将bind-address = 127.0.0.1前面加“#”注释掉,这样就可以允许其他机器远程访问本机mysql了;
>> grant all privileges on *.* to root@"%" identified by "123456" ;
//设置用户root,可以在远程访问mysql
>> select host,user from user;
//查询mysql中所有用户权限
关闭root用户远程访问权限
>> delete from user where user="root" and host="%" ;
//禁止root用户在远程机器上访问mysql
>> flush privileges ;
//修改权限之后,刷新MySQL的系统权限相关表方可生效
管理命令
- SHOW DATABASES
- 列出数据库列表
- USE 数据库名
- 选择要操作的数据库
- SHOW TABLES
- 显示指定数据库的所有表,使用该命令前需要使用 use 命令来选择要操作的数据库。
- SHOW COLUMNS FROM 数据表
- 显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。
- SHOW INDEX FROM 数据表
- 显示数据表的详细索引信息,包括PRIMARY KEY(主键)。