MySql中添加用户,新建数据库,用户授权,删除用户,修改密码
1.新建用户
// 登录MYSQL
@>mysql -u root -p
@>password
mysql> set names utf8;
// 创建本地用户
mysql> insert into mysql.user(Host,User,Password) values( "localhost","test",password("1234") );
// 创建远程用户
mysql> insert into mysql.user(Host,User,Password) values( "ip或%","test",password("test") );
2.为用户授权
// 登录MYSQL(有ROOT权限)。我里我以ROOT身份登录.
@>mysql -u root -p
@>password
//首先为用户创建一个数据库(testDB)
mysql> create database testDB;
//授权test用户拥有testDB数据库的所有权限。
mysql> grant all privileges on testDB.* to 'test'@'localhost' identified by '1234';
//刷新系统权限表
mysql> flush privileges;
指定部分权限给用户::
mysql> grant select,update on testDB.* to 'test'@'localhost' identified by '1234';
//刷新系统权限表。
mysql>flush privileges;
给root用户授权远程连接权限:
mysql> Grant all privileges on *.* to 'root'@'%' identified by 'password123' with grant option;
//刷新系统权限表。 mysql>flush privileges;
3.删除用户
@>mysql -u root -p
@>password
mysql> Delete FROM user Where User="test" and Host="localhost";
mysql> flush privileges;
//删除用户的数据库
mysql>drop database testDB;
4.修改指定用户密码
@>mysql -u root -p
@>password
mysql> update mysql.user set password=password('新密码') where User="test" and Host="localhost";
mysql> flush privileges;
5.列出所有数据库
mysql>show database;
6.切换数据库
mysql>use '数据库名';
7.列出所有表
mysql>show tables;
8.显示数据表结构
mysql>desc 表名;
9.删除数据库和数据表
mysql>drop database 数据库名;
mysql>drop table 数据表名;
忘记mysql root密码解决办法
1.编辑MySQL配置文件my.cnf
vi /etc/my.cnf #编辑my.cnf文件
找到[mysqld],在下面添加一行skip-grant-tables
[mysqld]
skip-grant-tables
:wq! #保存退出
service mysqld restart #重启MySQL服务
2.进入MySQL控制台
mysql -uroot -p #直接按回车,这时不需要输入root密码。
3.修改root密码
update mysql.user set password=password('123456') where User="root" and Host="localhost";
注意⚠️:Mysql5.7+版本 user表并没有password字段,按照如下修改:
update mysql.user set authentication_string=password('123456') where User="root" and Host="localhost";
flush privileges; #刷新系统授权表
grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;
4、取消/etc/my.cnf中的skip-grant-tables
vi /etc/my.cnf #编辑文件,找到[mysqld],删除skip-grant-tables这一行
:wq! #保存退出
5、重启mysql
service mysqld restart #重启mysql,这个时候mysql的root密码已经修改为123456
6、进入mysql控制台
mysql -uroot -p #进入mysql控制台
123456 #输入密码
7、远程Mysql无法链接,开放3306(默认)端口
vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306-j ACCEPT
#保存,并重启iptables(如果设置自动生效可忽略)
service iptables restart