怎么查看安装mysql设置的密码(Mysql新手必备-Mysql安装)
- 下载并安装MySQL官方的 Yum Repository
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
- 然后安装刚下载的rpm,如果已经有了mysql安装包,则可直接安装
- 安装mysql服务
- 查看Mysql状态
- 启动mysql服务(当出现下图绿色字样的: running 状态时说明mysql启动成功)
- 关闭mysql服务
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# yum -y install mysql-community-server
[root@localhost ~]# service mysqld status
[root@localhost ~]# service mysqld start
[root@localhost ~]# service mysqld stop
设置mysql用户名和密码
- 获取root用户初始密码刚安装的mysql只有root用户,此时需要先知道root用户的密码登录mysql才能进行其他操作。我们可以通过查看mysqld.log文件得到root用户的初始密码。
- 这里需要注意mysqld.log的位置
[root@localhost ~]# grep "password" /var/log/mysqld.log
[root@localhost ~]# use mysql
- 查看mysql的密码设置
mysql> use mysql
mysql> show variables like 'validate_password%'
- 修改mysql密码规则
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=6;
- 修改mysql下root用户的密码:
方式一: set 并设置密码永不过期
mysql> set password=password('123456'); # set设置
mysql> alter user 'root'@'localhost' password expire never;
mysql> flush privileges;
mysql> exit;
方式二: update用户密码
mysql> update user set password=password("123456") where user='root'; # update更新
mysql> flush privileges;
mysql> exit;
- 如果忘记了mysql的root用户密码,如何重置?
首先,你必须有linux的系统root权限,可以 sudo su
然后,使用Mysql的安全模式进入
service mysqld stop(要先将mysqld添加为系统服务)
mysqld_safe --skip-grant-tables &
或者
mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
接着重置密码:
# mysql
#5.6及以前
mysql> UPDATE mysql.user SET password=password('123456') WHERE user='root';
# 5.7;mysql.user表authentication_string字段替换了password字段;
mysql> UPDATE mysql.user SET authentication_string=password('123456') WHERE user='root';
mysql> flush privileges;
mysql> exit;
1. 查看端口,默认是3306
mysql> show global variables like 'port';
2. 修改端口,进入my.cnf文件,添加port=2206 ,然后重启服务
[root@localhost ~] vi /etc/my.cnf
[root@localhost ~] /etc/init.d/mysqld restart
create user tong identified by '123';
create user tong@localhost identified by '123'; 只能本地登录
mysql> create user tong@'192.168.206.0/255.255.255.0' identfied by '123'; 192.168.206.0/24 #网段
mysql> create user tong@'192.168.206.10' identfied by '123'; 只允许192.168.206.10该ip登录
create user tom@'%' identified by '123'; 所有能连接主机
- mysql用户授权,例如设置一些查看权限的用户
grant select on mydb.* to tong@'localhost'; #授权查看的权限
show grants for tong\G
*************************** 1. row ***************************
Grants for tong@%: GRANT USAGE ON *.* TO 'tong'@'%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257'
*************************** 2. row ***************************
Grants for tong@%: GRANT SELECT ON `mydb`.* TO 'tong'@'%'
远程主机授权
grant all on mydb.* to tom@'192.168.206.10' identified by '123';
grant all on mydb.* to tom@'%' identified by '123';
授权selecet和insert权限
grant select,insert on mydb.* to jerry@'localhost' identified by '123';
授权某个特定的表访问权限
grant select,insert on mydb.test to tom@'localhost' identified by '123';
drop user tong;
drop user tong@'192.168.206.10';
revoke 权限 on 库.表 from 用户@主机;
revoke select on mydb.* from tong'localhost';
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com