設置更改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密碼