13.1 设置更改root密码
时间:2022-04-27
本文章向大家介绍13.1 设置更改root密码,主要内容包括设置更改root密码目录概要、设置更改root密码、不知道mysql的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 password=password('aminglinux') where user='root';
设置更改root密码
- root用户是mysql的超级管理员用户,和linux系统的root用户类似,不过和Linux的不一样
- 默认mysql的 root 用户密码是空的,直接就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
- 为了方便使用mysql服务,将mysql目录加入到环境变量里
- 打开系统,查看mysql是否启动
[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 ~]#
- 若是没有启动mysql的话,将mysql启动起来
/etc/init.d/mysqld start
- 在启动mysql后,使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错
[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 ~]#
- 若想要这个命令直接运行,需要把PATH做一个更改
[root@hanfeng ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@hanfeng ~]#
- 这时再来使用mysql -uroot命令就会发现可以使用了
- 退出mysql输入 quit 即可
[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 ~]#
- 若想要变量永久生效,还需要将export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile
[root@hanfeng ~]# vim /etc/profile
将内容放到配置文件的最后面,使命令永久生效
export PATH=$PATH:/usr/local/mysql/bin/
保存退出
- 假设若是没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,因为变量还没有生效,想要这个变量生效,在配置文件中加入命令后,还需要执行source /etc/profile 命令,重新加载
[root@hanfeng ~]# source /etc/profile
[root@hanfeng ~]#
- 一般是使用mysql -uroot -p命令
- -p,表示指定密码
- 密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
[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 ~]#
- 退出mysql,输入 quit 即可
- 设置mysql密码,命令为mysqladmin -uroot passwd 'hanfeng.1' 在 ' ' 为密码
[root@hanfeng ~]# mysqladmin -uroot password 'hanfeng.1'
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
- 在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你 你现在密码在当前命令行显示出来了,这样不太安全
- 这时在想直接登录mysql,就会提示需要输入密码了
[root@hanfeng ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hanfeng ~]#
- 在使用-p,并输入密码就可以正常的进入到mysql命令行了
[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密码,去更改密码
- 若是这时知道mysql密码,去修改mysql密码,看到输出的提示信息不用去理会
- 格式
- mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
[root@hanfeng ~]# mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
- 指定新密码去登录,当然也可以不明文指定密码,知道-p回车,输入密码登录也行
[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 ~]#
- 在明文指定密码的时候,密码可以加单引号,也可以不加单引号,建议加上单引号,防止密码有特殊符号的存在——>(若是不加单引号,而密码中又有特殊符号,就有可能会不识别)
不知道mysql的root密码,去更改密码
- 在不知道mysql的root用户密码的时候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
- skip-grant ,表示忽略授权,也就是说操作mysql的时候不需要用户名和密码了,能直接登录
[root@hanfeng ~]# vim /etc/my.cnf
在[mysqld]下面
加入一行
skip-grant
保存退出
- 在更改配置文件后,重启mysql服务
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@hanfeng ~]#
- 这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
[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>
- 在登录进mysql后,还需要更改一个表,因为用户名和密码是存在于一个mysql库里面的,使用 use mysql; 切换库,在切换到mysql库里面,然后去更改一个存用户名密码的user表
- use mysql; 切换库
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>
- 查看user表,输入select * from user; 命令,会看到输出乱七八糟的乱码,里面存放的就是用户名和密码,还有权限和授权等信息
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 |
等等等,都是乱码
- 查看password表,会看到密码都是加密的
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>
- 更改user表,使用update user set password=password('aminglinux') where user='root'; 命令
- update user set password=password('aminglinux') where user='root';
- 密码字段 函数 //用于加密密码 高亮部分:为条件语句
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>
- 提示说4行修改完毕,即使有些行是空的
- 这样密码就更改成功了,输入quit退出数据库即可
mysql> quit
Bye
- 再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant——>若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
[root@hanfeng ~]# vim /etc/my.cnf
在[mysqld]下面删除刚添加的
删除skip-grant
保存退出
- 重启mysql服务
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@hanfeng ~]#
- 重启完之后,再用新的密码测试下,会看到新的密码可以登录
[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 ~]#
- 这样就是成功更改mysql密码
- 1578: [Usaco2009 Feb]Stock Market 股票市场
- webp图片实践之路
- 3522: [Poi2014]Hotel
- 3299: [USACO2011 Open]Corn Maze玉米迷宫
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
- 1632: [Usaco2007 Feb]Lilypad Pond
- 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
- Java设计模式(七)Decorate装饰器模式
- 1623: [Usaco2008 Open]Cow Cars 奶牛飞车
- 1622: [Usaco2008 Open]Word Power 名字的能量
- 3297: [USACO2011 Open]forgot
- 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂
- 1741: [Usaco2005 nov]Asteroids 穿越小行星群
- 3298: [USACO 2011Open]cow checkers
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- You-Get 使用方法
- 接口测试 Mock 实战 | 结合 jq 完成批量化的手工 Mock
- 在Angular应用的child Component里同时使用@Input和@Output
- Angular应用里的@Input和@Output注解使用方法介绍
- K8S Ingress使用|常见问题列表
- 部署Tomcat及负载均衡
- Jenkins常用插件Publish Over SSH
- Zabbix 4.0配置监控Linux客户端
- esp8266+oled+DHT 11温湿度显示
- esp8266+oled 显示时间日期
- esp8266+DHT11温湿传感器 制作web室内温度计
- Linux端口转发的几种常用方法
- kali破解wifi密码
- 面向对象的7种设计原则(7)-开闭原则
- [数据库介绍]一站式表达谱数据分析