|NO.Z.00087|——————————|^^ 操作 ^^|——|Linux&MySQL.V02|——|部署MySQL|找回root密码|
一、 MySQL 管理命令:部署mysql
### --- 部署MySQL
~~~		服务端:20.20.20.21
~~~		客户端:20.20.20.22
[root@server21 ~]# yum install -y mysql-server mysql
[root@server21 ~]# chkconfig mysqld on
[root@server21 ~]# service mysqld start
[root@server21 ~]# netstat -antp
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2048/mysqld  ### --- 查看主配置文件位置
[root@localhost ~]# cat /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid### --- 该目录下放着大量的数据库;我们自己保存的时候是.sql文件,默认保存不是.sql文件
[root@server21 ~]# ls /var/lib/mysql/mysql  				  // 启动文件
[root@localhost ~]# cat /var/run/mysqld/mysqld.pid 
2048
[root@server21 ~]# ps aux |grep 2048                           // 2480是mysql的启动进程
mysql      2048  0.0  1.4 377036 27732 pts/2    Sl   09:34   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root       2072  0.0  0.0 103312   876 pts/2    S+   09:39   0:00 grep 2048### --- 为mysql设置root账号密码
[root@server21 ~]# mysqladmin -uroot password 123456
 
### --- 登录
[root@server21 ~]# mysql -u root -p123456 -P 3306 -S /var/lib/mysql/mysql.sock 
mysql>
[root@server21 ~]# mysql -u root -p123456                      // 默认写到这个位置即可,端口和sock文件位置是默认的地址
mysql>二、创建登录用户
### --- 创建登录用户
~~~		%:指任意的远程终端'localhost':只允许本地,
~~~		也可以写单独的IP地址'20.20.20.22'或网段:'20.20.20.0/24'
~~~		identified:申明该账号所使用的密码
mysql> create user zhangsan@'%' identified by '123456';        // 允许所有主机登录
Query OK, 0 rows affected (0.00 sec)三、测试用户登录
### --- 测试用户登录
~~~		通过客户端连接数据库
[root@server22 ~]# yum install -y mysql
[root@server22 ~]# mysql -uzhangsan -p123456 -h 20.20.20.21
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)四、用户为自己更改密码
### --- 用户为自己更改密码
mysql> set password=password('654321');
Query OK, 0 rows affected (0.00 sec)
[root@server22 ~]# mysql -uzhangsan -p123456 -h 20.20.20.21
ERROR 1045 (28000): Access denied for user 'zhangsan'@'20.20.20.22' (using password: YES)
[root@server22 ~]# mysql -uzhangsan -p654321 -h 20.20.20.21
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)五、root 用户为其他用户找回密码
### --- root 用户为其他用户找回密码
[root@server21 ~]# mysql -u root -p123456
mysql> set password for zhangsan@'%'=password('123123');
Query OK, 0 rows affected (0.00 sec)
[root@server22 ~]# mysql -uzhangsan -p123123 -h 20.20.20.21
mysql> 六、root 找回自己的密码并修改
### --- root 找回自己的密码并修改
~~~		关闭数据库,修改主配置文件(/etc/my.cnf)添加:skip-grant-tables
[root@server21 ~]# service mysqld stop
[root@server21 ~]# vim /etc/my.cnf
skip-grant-tables                                             // 跳过授权表,不进行密码认证### --- 启动数据库,空密码登录并修改密码
~~~		更新mysql库的user表,新密码为多少,user表示用户
[root@server21 ~]# service mysqld start
[root@server21 ~]# mysql -uroot                               // 跳过密码直接登录
mysql> update mysql.user set password=password('123123') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0### --- 删除 skip-grant-tables,重启数据库验证新密码
[root@server21 ~]# vim /etc/my.cnf  
#skip-grant-tables
[root@server21 ~]# service mysqld restart
[root@server21 ~]# mysql -uroot                               // 无密码登录失败
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@server21 ~]# mysql -uroot -p123123                      // 密码登录OK
mysql> Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor
 
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号 
