1.连接数据库
Windows
1.下载phpstudy,开启数据库服务。
地址:https://www.xp.cn/
2.选择好配置,找到phpstudy所在文件夹找到MySQL下的bin文件夹,并复制路径。
3.打开Windows终端,切换至该路径下。
4.输入命令,进入
mysql -uroot -p
密码在phpstudy开启数据库服务中会设置,默认一般是123456.
Linux
我的版本是centOS7,
本机MySQL版本:
mysql Ver 14.14 Distrib 5.6.51, for Linux (x86_64) using EditLine wrapper
1.安装好MySQL后先启动服务
systemctl start mysqld
2.mysql -uroot
:无密码登入MySQL
3.help
获取帮助
2.对用户操作
在Linux下操作
查询用户
use mysql;
:切换到mysql库下
select user,password from user;
:查询user,password从mysql库下的user表中。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,password from user;
+-------+-------------------------------------------+
| user | password |
+-------+-------------------------------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | |
| root | |
| root | |
| | |
| | |
| hello | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 |
+-------+-------------------------------------------+
7 rows in set (0.00 sec)
创建用户
1.create user 'xiaohei'@'localhost'; //无密码创建xiaohei用户
2.create user 'xiaohei'@'localhost' identified by 'password';
//有密码创建
mysql密码必须保证大小写+英文+数字+符号(>=14)
在授权数据库的同时,如果对应的用户不存在,那么mysql将会自动创建对应的用户。
mysql> grant all on security.* to xiaohei identified by '123456';
)
删除用户
drop user xiaohei@localhost;
重命名用户
rename user OldName to NewName;
授权用户
grant
权限 on
数据库名.表名 to
用户名@”本地回环地址或localhost
” identified by
“密码”
grant all privileges on security.* to xiaohei@localhost identified by 'root123';
授权时privileges关键字可省.
给远程用户授权
grant 权限 on 数据库名.* to 用户名@’远程主机地址或对应的主机名’ identified by “密码”
grant all privileges on test.* to xiaohei@'%' identified by '123';
上述命令比较危险
我们可以稍微缩小一下用户能够连接数据库的IP地址范围,比如,只允许用户通过192.168网段的地址连接数据库,示例语句如下
grant all privileges on test.* to xiaohei@'192.168.%.%' identified by '123';
上述命令授权后,需要使用如下命令刷新
FLUSH PRIVILEGES
另外还有些一般的授权
只授权用户对于某个数据库的查询权限,示例如下。
grant select privileges on test.* to xiaohei@'192.168.%.%';
授权用户某个数据库的多个权限,比如,授权用户
grant insert,delete,update,select on test.* to xiaohei@'192.168.%.%';
还可以将同样的权限同时授权于多个用户,示例如下
grant select on hellodb.* to xiaohei@localhost,xiaobai@localhost;
如果想要只对某张表的某个字段授权,可以使用如下语句
grant select (name,age) on test.students to xiaohei@localhost;
如果test数据库中有一张表的名称为test1,同时,test数据库中有一个函数也叫test1,那么,此时如果管理员只想将test1函数的权限授予xiaohei用户,而不是想将表的权限授予xiaohei用户,该怎么办?
我们可以通过function关键字指明被操作的对象为函数,而不是表。
grant execute on function test.test1 to xiaohei@'192.168.%.%';
表示授权xiaohei对test数据库中test1函数拥有执行权限。
查看授权
show grants for 用户名;
例如:show grants for xiaohei@localhost;
删除授权/撤销授权
常用语句
revoke "要移除的权限" on 数据库.表 from 用户@host;
MySQL修改密码操作
方法1:
mysqladmin -u用户名 -p旧密码 password 新密码
mysqladmin -uroot -p123456 password 123;
方法2:
使用root
用户登录以后,使用SET PASSWORD
命令设置用户密码。
set password for 'root'@localhost= password('123');
方法3:
update mysql.user set password = password('123') where user = 'root'
flush privileges //刷新特权
忘记密码操作
Windows下
1.在my.ini下添加skip-grant-tables
2.直接利用命令mysql -uroot -p
进入mysql
3.利用update
命令更改root
密码
4.flush priviles
5.删除my.ini
下的`skip-grant-tables
6.重新启动
mariadb忘记root密码:
第一步:解决无验证登陆问题
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> alter user root@localhost identified via mysql_native_password using password ('my secret password');
MariaDB [(none)]> flush privileges;
第二步: 修改密码:
MariaDB [(none)]> flush privileges;
MariaDB [mysql]> set password for root@localhost=password('123456');
MariaDB [(none)]> flush privileges;