设置更改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命令就会发现可使用了
[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命令
- 密码为空的时候,直接回车就可进入到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表
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密码