Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >13.1 设置更改root密码

13.1 设置更改root密码

作者头像
运维小白
发布于 2018-02-06 08:10:01
发布于 2018-02-06 08:10:01
3.5K00
代码可运行
举报
文章被收录于专栏:运维小白运维小白
运行总次数:0
代码可运行

设置更改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
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
/etc/init.d/mysqld start
  1. 在启动mysql后,使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@hanfeng ~]# 
  1. 这时再来使用mysql -uroot命令就会发现可以使用了
  • 退出mysql输入 quit 即可
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# source /etc/profile
[root@hanfeng ~]# 
  1. 一般是使用mysql -uroot -p命令
  • -p,表示指定密码
  1. 密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hanfeng ~]# 
  1. 在使用-p,并输入密码就可以正常的进入到mysql命令行了
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# vim /etc/my.cnf

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

保存退出
  1. 在更改配置文件后,重启mysql服务
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
mysql> quit
Bye
  1. 再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant——>若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# vim /etc/my.cnf

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

保存退出
  1. 重启mysql服务
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 重启完之后,再用新的密码测试下,会看到新的密码可以登录
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Linux基础(day54)
13.1 设置更改root密码 设置更改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
运维小白
2018/02/06
9270
Linux基础(day55)
13.4 mysql用户管理 mysql用户管理目录概要 grant all on . to 'user1' identified by 'passwd'; grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd'; grant all on db1.* to 'user3'@'%' identified by 'passwd'; show grants; show grants for use
运维小白
2018/02/06
1K0
Linux基础(day61)
17.1 MySQL主从介绍 MySQL主从介绍 ---- MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的 MySQL主从是基于binlog的,主上须开启binlog才能进行主从。 binlog,其实就是一个文件,文件里记录了一些日志,文件是 二进制文件,无法cat 主从过程大致有3个步骤 1)主将更改操作记录到binlog里 2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylo
运维小白
2018/02/07
6770
Linux基础(day61)
17.4 配置从
主从配置 - 从上操作 安装mysql 查看my.cnf,配置server-id=132,要求和主不一样 修改完配置文件后,启动或者重启mysqld服务 把主上aming库同步到从上 可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库 mysql -uroot stop slave; change master to master_host='', master_user='repl', master_password='', master_log_file='
运维小白
2018/02/07
7140
13.4 mysql用户管理
mysql用户管理目录概要 grant all on . to 'user1' identified by 'passwd'; grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd'; grant all on db1.* to 'user3'@'%' identified by 'passwd'; show grants; show grants for user2@192.168.133.
运维小白
2018/02/06
8470
你确定你的MySQL足够安全吗?
对于任何一种数据库来说,安全问题都是非常重要的。如果数据库出现安全漏洞,轻则数据被窃取,重则数据被破坏,这些后果对于一些重要的数据库都是非常严重的。下面来从操作系统和数据库两个层对MySQL的安全问题进行讨论。
Bug开发工程师
2018/11/06
1K0
学习笔记0521----mysql管理
MySQL的安装路径为:/usr/local/mysql/,可执行文件在bin目录下,此目录并未添加到系统的环境变量中,所以要使用mysql命令,需要把 /usr/local/mysql/bin/ 目录添加到系统的环境变量中。
嘻哈记
2020/11/24
1.1K0
设置mysql用户密码(5.6/5.7)、远程连接数据库、常用命令
首次直接使用mysql会提示‘该命令不存在’,原因是还没有将该命令加入环境变量,如果要使用该命令,需要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量:
阿dai学长
2019/04/03
3.9K0
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
问题 [root@node1 text]# mysql -uroot -p123456 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) [root@node1 text]# 停止mysql [root@node1 text]# service mysqld stop Stopping mysqld: [
程裕强
2022/05/06
8450
MySQL忘记root密码处理
如果忘记密码,对于MySQL而言处理起来也相对比较简单。但需要修改配置,重启数据库。可以按照如下步骤处理。
俊才
2020/01/17
4.6K0
Centos 6.x 安装配置MySQL
service mysqld start chkconfig mysqld on
程裕强
2022/05/06
4140
Linux 环境下 MySQL 5.7 root密码忘记-解决方法
输入密码后提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
zinyan.com
2022/12/07
1.7K0
MySQL数据库基本语句
MySQL数据库系统是一个典型的C/S(客户端/服务器)架构的应用,要访问MySQL数据库需要使用专门的客户端软件。在Linux系统中,最简单、易用的MySQL客户端软件是其自带的mysql命令工具。
星哥玩云
2022/08/18
5K0
MySQL数据库基本语句
MySQL8.0 双密码机制:解决应用程序用户不停机修改密码问题
在数据库管理中,定期更新密码是确保系统安全的重要手段。然而,如何在不影响现有连接的情况下平滑地切换密码,避免系统停机,始终是一个挑战。MySQL 8.0 引入的“双密码”机制为这种需求提供了有效的解决方案,使得密码更新过程能够无缝进行。
俊才
2024/11/28
3090
MySQL8.0 双密码机制:解决应用程序用户不停机修改密码问题
13.2 连接mysql
连接mysql 本地连接——>即使没有指定,但默认使用sock连接,使用/tmp/mysql.sock连接 mysql -uroot -p123456 //输入用户名和密码连接本机 使用ip端口连接远程机器 mysql -uroot -p123456 -h[远程mysql主机IP] -P[端口] [root@hf-01 ~]# mysql -uroot -phanfeng -h127.0.0.1 -P3306 Warning: Using a password on the command lin
运维小白
2018/02/06
8010
16.5/16.6/16.7 配置Tomcat虚拟主机
16.5/16.6/16.7 配置Tomcat虚拟主机目录概要 vim /usr/local/tomcat/conf/server.xml 其中<Host>和</Host>之间的配置为虚拟主机配置部分,name定义域名, appBase定义应用的目录,Java的应用通常是一个war的压缩包,你只需要将war的压缩包放到appBase目录下面即可。刚刚阿铭访问的Tomcat默认页其实就是在appBase目录下面,不过是在它子目录ROOT里。 增加虚拟主机,编辑server.xml,在</Host>下面增加如下
运维小白
2018/02/06
1.6K0
16.5/16.6/16.7 配置Tomcat虚拟主机
二进制包安装MySQL_5.7 原
(adsbygoogle = window.adsbygoogle || []).push({});
阿dai学长
2019/04/03
1.1K0
排障集锦:九九八十一难之第七难!mysql数据库登录密码忘记了
vim /etc/my.cnf 进入到配置文件 skip-grant-tables 加上此字段 跳过密码直接登录 登录后对mysql库中的user表中authentication_string 密码字段进行更改
不吃小白菜
2020/09/03
9260
一键安装MySQL 5.7与密码及策略修改
一、一键安装MySQL脚本 [root@linuxidc ~]# cat InstallMysql01.sh #!/bin/bash #2018-10-13 #旅行者-Travel #1.安装wget yum -y install  wget #2、下载mysql的yum源 URL="https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm" wget $URL -P /etc/yum.repos.d/ yum -y install
星哥玩云
2022/08/17
6890
mysql 更改密码 alter_MySQL修改账号密码方法大全「建议收藏」
在日常使用数据库的过程中,难免会遇到需要修改账号密码的情景,比如密码太简单需要修改、密码过期需要修改、忘记密码需要修改等。本篇文章将会介绍需要修改密码的场景及修改密码的几种方式。
全栈程序员站长
2022/09/21
4.7K0
相关推荐
Linux基础(day54)
更多 >
领券
一站式MCP教程库,解锁AI应用新玩法
涵盖代码开发、场景应用、自动测试全流程,助你从零构建专属AI助手
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档