Linuxでmysqlのrootユーザーパスワードを変更する方法


1.まず、サーバが安全な状態であることを確認します.つまり、MySQLデータベースに任意に接続できる人はいません.(無視可能)
MySQLのrootパスワードを再設定している間、MySQLデータベースは完全にパスワード保護されていない状態であるため、他のユーザーも任意にMySQLの情報を登録・変更することができる.MySQLの外部ポートを閉鎖し、Apacheとすべてのユーザープロセスを停止する方法でサーバの準セキュリティ状態を実現できます.最も安全な状態は、サーバーのコンソール上で操作し、ネットワークケーブルを抜くことです.
2.MySQLのログイン設定を変更するには:vim /etc/my.cnf
[mysqld]のセグメントにskip-grant-tables、例えば[mysqld]datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip-grant-tablesはvimを保存して終了します.
3.mysqldを再起動する/etc/init.d/mysqld restart
Stopping MySQL: [ OK ] Starting MySQL: [ OK ]
4.MySQLのrootパスワードをログインして変更する/usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or\g. Your MySQL connection id is 3 to server version: 3.23.56 Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer. 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> UPDATE user SET Password = password ( ‘new-password’ ) WHERE User = ‘root’ ; Query OK, 0 rows affected (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 0 mysql> flush privileges ; Query OK, 0 rows affected (0.01 sec) mysql> quit Bye
5.MySQLのログイン設定を変更するvim /etc/my.cnf
さっき[mysqld]のセグメントに追加したskip-grant-tablesを削除またはコメント保存しvimを終了します.
6.mysqldを再起動/etc/init.d/mysqld restart
Stopping MySQL: [ OK ] Starting MySQL: [ OK ] Work for fun,Live for love!