前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >13.1 设置更改root密码

13.1 设置更改root密码

作者头像
运维小白
发布2018-02-06 16:10:01
2.9K0
发布2018-02-06 16:10:01
举报
文章被收录于专栏:运维小白运维小白

设置更改root密码目录概要

  • /usr/local/mysql/bin/mysql -uroot
  • 更改环境变量PATH,增加mysql绝对路径
  • mysqladmin -uroot password '123456'
  • mysql -uroot -p123456
  • 密码重置
  • vi /etc/my.cnf//增加skip-grant
  • 重启mysql服务 /etc/init.d/mysqld restart
  • mysql -uroot
  • use mysql;
  • update user set password=password('aminglinux') where user='root';

设置更改root密码

  • root用户是mysql的超级管理员用户,和linux系统的root用户类似,不过和Linux的不一样
  • 默认mysql的 root 用户密码是空的,直接就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
  • 为了方便使用mysql服务,将mysql目录加入到环境变量里
  1. 打开系统,查看mysql是否启动
代码语言:javascript
复制
[root@hanfeng ~]# ps aux |grep mysql
root      1659  0.0  0.1 115392  1712 ?        S    21:29   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/hanfeng.pid
mysql     2260  0.0 45.3 973548 458428 ?       Sl   21:29   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/hanfeng.err --pid-file=/data/mysql/hanfeng.pid
root      2386  0.0  0.0 112676   984 pts/0    R+   22:06   0:00 grep --color=auto mysql
[root@hanfeng ~]# 
  1. 若是没有启动mysql的话,将mysql启动起来
代码语言:javascript
复制
/etc/init.d/mysqld start
  1. 在启动mysql后,使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@hanfeng ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@hanfeng ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@hanfeng ~]# 
  1. 若想要这个命令直接运行,需要把PATH做一个更改
代码语言:javascript
复制
[root@hanfeng ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@hanfeng ~]# 
  1. 这时再来使用mysql -uroot命令就会发现可以使用了
  • 退出mysql输入 quit 即可
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 若想要变量永久生效,还需要将export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile
代码语言:javascript
复制
[root@hanfeng ~]# vim /etc/profile

将内容放到配置文件的最后面,使命令永久生效
export PATH=$PATH:/usr/local/mysql/bin/

保存退出
  1. 假设若是没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,因为变量还没有生效,想要这个变量生效,在配置文件中加入命令后,还需要执行source /etc/profile 命令,重新加载
代码语言:javascript
复制
[root@hanfeng ~]# source /etc/profile
[root@hanfeng ~]# 
  1. 一般是使用mysql -uroot -p命令
  • -p,表示指定密码
  1. 密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 退出mysql,输入 quit 即可
  2. 设置mysql密码,命令为mysqladmin -uroot passwd 'hanfeng.1' 在 ' ' 为密码
代码语言:javascript
复制
[root@hanfeng ~]# mysqladmin -uroot password 'hanfeng.1'
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]# 
  1. 在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你 你现在密码在当前命令行显示出来了,这样不太安全
  2. 这时在想直接登录mysql,就会提示需要输入密码了
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hanfeng ~]# 
  1. 在使用-p,并输入密码就可以正常的进入到mysql命令行了
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 

知道mysql的root密码,去更改密码

  1. 若是这时知道mysql密码,去修改mysql密码,看到输出的提示信息不用去理会
  • 格式
    • mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
代码语言:javascript
复制
[root@hanfeng ~]# mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]# 
  1. 指定新密码去登录,当然也可以不明文指定密码,知道-p回车,输入密码登录也行
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot -p 'hanfeng'
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@hanfeng ~]# mysql -uroot -p'hanfeng'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 在明文指定密码的时候,密码可以加单引号,也可以不加单引号,建议加上单引号,防止密码有特殊符号的存在——>(若是不加单引号,而密码中又有特殊符号,就有可能会不识别)

不知道mysql的root密码,去更改密码

  1. 在不知道mysql的root用户密码的时候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
  • skip-grant ,表示忽略授权,也就是说操作mysql的时候不需要用户名和密码了,能直接登录
代码语言:javascript
复制
[root@hanfeng ~]# vim /etc/my.cnf

在[mysqld]下面
加入一行
skip-grant

保存退出
  1. 在更改配置文件后,重启mysql服务
代码语言:javascript
复制
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
  1. 在登录进mysql后,还需要更改一个表,因为用户名和密码是存在于一个mysql库里面的,使用 use mysql; 切换库,在切换到mysql库里面,然后去更改一个存用户名密码的user表
  • use mysql; 切换库
代码语言:javascript
复制
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> 
  1. 查看user表,输入select * from user; 命令,会看到输出乱七八糟的乱码,里面存放的就是用户名和密码,还有权限和授权等信息
代码语言:javascript
复制
mysql> select * from user;
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| Host      | User | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | 

等等等,都是乱码
  1. 查看password表,会看到密码都是加密的
代码语言:javascript
复制
mysql> select password from user where user=’root’;
ERROR 1054 (42S22): Unknown column '’root’' in 'where clause'
mysql> select password from user where user='root';
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *406D1994F8340A1442C5090388944CCB985BA3DE |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> 
  1. 更改user表,使用update user set password=password('aminglinux') where user='root'; 命令
  • update user set password=password('aminglinux') where user='root';
    • 密码字段 函数 //用于加密密码 高亮部分:为条件语句
代码语言:javascript
复制
mysql> update user set password=password('aminglinux') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> 
  1. 提示说4行修改完毕,即使有些行是空的
  2. 这样密码就更改成功了,输入quit退出数据库即可
代码语言:javascript
复制
mysql> quit
Bye
  1. 再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant——>若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
代码语言:javascript
复制
[root@hanfeng ~]# vim /etc/my.cnf

在[mysqld]下面删除刚添加的
删除skip-grant

保存退出
  1. 重启mysql服务
代码语言:javascript
复制
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 重启完之后,再用新的密码测试下,会看到新的密码可以登录
代码语言:javascript
复制
[root@hanfeng ~]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@hanfeng ~]# 
  1. 这样就是成功更改mysql密码
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 设置更改root密码目录概要
  • 设置更改root密码
    • 知道mysql的root密码,去更改密码
      • 不知道mysql的root密码,去更改密码
      相关产品与服务
      云数据库 MySQL
      腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档