web-dev-qa-db-ja.com

MySQLの再インストールまたはそのルートパスワードのリセット

2つのUbuntu 16.04で、LAMPの下でMySQL 5.7.19を実行しています。これはAWS EC2インスタンスです。

MySQLをインストールしてからしばらく経ちましたが、使用していませんので、再インストールしても問題はありません。ルートパスワードに入力したものが機能しません。

私は試した

 Sudo /etc/init.d/mysql stop
 Sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
Sudo  mysql -u root

をもたらしました

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

次に、 here で概説されている手順を試しました。入ったとき

 Sudo apt-get install mysql-server

わかった

Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-generic : Depends: linux-headers-4.4.0-119-generic but it is     not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

私が入るとき

ps -aux | grep mysql

結果は

ubuntu   27152  0.0  0.0  12948   984 pts/1    S+   12:30   0:00 grep --color=auto mysql

私が入るとき

 mysql -u root -p

私は得る

Enter password:

したがって、mysqlはアンインストールされておらず、再構成できません。

更新1:

走ったとき

Sudo dpkg-reconfigure mysql-server-5.7

わかった

update-alternatives: warning: forcing reinstallation of alternative 
/etc/mysql/mysql.cnf because link group my.cnf is broken
update-alternatives: warning: not replacing /etc/mysql/my.cnf with a link
mysql_upgrade: [ERROR] unknown option '--~'
Upgrade process encountered error and will not continue.

 mysql -u root -p

結果として

Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

編集2

関連する結果で以下を試しました。

ubuntu@ip-10-0-1-73:~$ Sudo apt autoremove --purge mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-generic : Depends: linux-headers-4.4.0-119-generic but it is     not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify     a solution).
ubuntu@ip-10-0-1-73:~$ Sudo apt install mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-generic : Depends: linux-headers-4.4.0-119-generic but it is     not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify     a solution).

ランニング

apt-get -f install

何も変更しませんでした。

編集3:

私が使用する場合

Sudo mysql -u root

mysqlを使用します。 -pスイッチを使用すると、パスワードとして何でも受け入れます。しかし、私が入るとき

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('newPassword') WHERE User='root';

私は得る

Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

Warning (Code 1681): 'PASSWORD' is deprecated and will be removed in a future release.

それから私は入った

FLUSH PRIVILEGES;

そして、次のメッセージを受け取りました

Query OK, 0 rows affected (0.00 sec)

その後、Sudoを使用しない場合、終了しても同じ結果が得られます

 mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

編集4:

次の結果を試してみました。

$ Sudo apt install linux-headers-4.4.0-119-generic
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-4.4.0-119-generic : Depends: linux-headers-4.4.0-119 but it     is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
ubuntu@ip-10-0-1-73:~$ Sudo apt-get -f install linux-headers-4.4.0-119-    generic
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-4.4.0-119-generic : Depends: linux-headers-4.4.0-119 but it     is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
$ Sudo apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer     required:
  linux-headers-4.4.0-64 linux-headers-4.4.0-64-generic linux-headers-4.4.0-    66 linux-headers-4.4.0-66-generic linux-headers-4.4.0-70 linux-headers-4.4.0-70-generic
  linux-headers-4.4.0-71 linux-headers-4.4.0-71-generic linux-headers-4.4.0-    72 linux-headers-4.4.0-72-generic linux-headers-4.4.0-75 linux-headers-4.4.0-75-    generic
  linux-headers-4.4.0-78 linux-headers-4.4.0-78-generic linux-headers-4.4.0-    79 linux-headers-4.4.0-79-generic linux-headers-4.4.0-81 linux-headers-4.4.0-81-generic
  linux-headers-4.4.0-83 linux-headers-4.4.0-83-generic linux-headers-4.4.0-87 linux-headers-4.4.0-87-generic linux-headers-4.4.0-89 linux-headers-4.4.0-89-generic
  linux-headers-4.4.0-91 linux-headers-4.4.0-91-generic linux-headers-4.4.0-92 linux-headers-4.4.0-92-generic linux-image-4.4.0-112-generic
  linux-image-4.4.0-64-generic linux-image-4.4.0-66-generic linux-image-    4.4.0-70-generic linux-image-4.4.0-71-generic linux-image-4.4.0-72-generic
  linux-image-4.4.0-75-generic linux-image-4.4.0-78-generic linux-image-4.4.0-79-generic linux-image-4.4.0-81-generic linux-image-4.4.0-83-generic
  linux-image-4.4.0-87-generic linux-image-4.4.0-89-generic linux-image-4.4.0-91-generic linux-image-4.4.0-92-generic linux-image-4.4.0-97-generic
Use 'Sudo apt autoremove' to remove them.
The following additional packages will be installed:
  linux-headers-4.4.0-119 linux-headers-4.4.0-119-generic
The following NEW packages will be installed:
  linux-headers-4.4.0-119 linux-headers-4.4.0-119-generic
0 upgraded, 2 newly installed, 0 to remove and 224 not upgraded.
10 not fully installed or removed.
Need to get 0 B/10.8 MB of archives.
After this operation, 78.3 MB of additional disk space will be used.
Do you want to continue? [Y/n]
(Reading database ... 512472 files and directories currently installed.)
Preparing to unpack .../linux-headers-4.4.0-119_4.4.0-119.143_all.deb ...
Unpacking linux-headers-4.4.0-119 (4.4.0-119.143) ...
dpkg: error processing archive /var/cache/apt/archives/linux-headers-4.4.0-    119_4.4.0-119.143_all.deb (--unpack):
 unable to create '/usr/src/linux-headers-4.4.0-    119/include/linux/cpufeature.h.dpkg-new' (while processing './usr/src/linux-        headers-4.4.0-119/include/linux/cpufeature.h'): No space left on device
No apport report written because the error message indicates a disk full     error
                                                                                      dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Preparing to unpack .../linux-headers-4.4.0-119-generic_4.4.0-    119.143_AMD64.deb ...
Unpacking linux-headers-4.4.0-119-generic (4.4.0-119.143) ...
dpkg: error processing archive /var/cache/apt/archives/linux-headers-4.4.0-    119-generic_4.4.0-119.143_AMD64.deb (--unpack):
 unable to create '/usr/src/linux-headers-4.4.0-119-    generic/include/config/rtc/drv/88pm860x.h.dpkg-new' (while processing     './usr/src/linux-headers-4.4.0-119-generic/include/config/rtc/drv/88pm860x.h'):     No space left on device
No apport report written because the error message indicates a disk full     error
                                                                                  dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Errors were encountered while processing:
 /var/cache/apt/archives/linux-headers-4.4.0-119_4.4.0-119.143_all.deb
 /var/cache/apt/archives/linux-headers-4.4.0-119-generic_4.4.0-    119.143_AMD64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

$df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              498080       0    498080   0% /dev
tmpfs             101440   12068     89372  12% /run
/dev/xvda1       8117828 5515716   2166836  72% /
tmpfs             507184       0    507184   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             507184       0    507184   0% /sys/fs/cgroup
tmpfs             101444       0    101444   0% /run/user/1000
3
OtagoHarbour

最後にそれを整理しました。

まず、my.cnf.bakのすべてのインスタンスを見つけて削除します。 /etc/alternatives/my.cnf、/var/lib/dpkg/alternatives/my.cnf、および/etc/mysql/conf.dも削除しました(ただし、バックアップしました)。それから

Sudo apt-get remove --purge mysql-\*
Sudo apt-get install mysql-server mysql-client

それでもまだ

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

これはしばしば解決されます

Sudo service mysql start
0
OtagoHarbour

MySQLパスコードをリセットするには、「 B.5.3.2.2ルートパスワードのリセット:UnixおよびUnix-Like Systems 」の手順に従ってください。

通常のkillkill -9ではなく)をmysqldプロセスに送信して、MySQLサーバーを停止します。次のコマンドで.pidファイルの実際のパス名を使用します。

kill `cat /mysql-data-directory/Host_name.pid`

Catコマンドで(逆引用符ではなく)バックティックを使用します。これらにより、catの出力がkillコマンドに置き換えられます。

1行にパスワード割り当てステートメントを含むテキストファイルを作成します。パスワードを使用するパスワードに置き換えます。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

ファイルを保存します。この例では、ファイルに/home/me/mysql-initという名前を付けることを想定しています。ファイルにはパスワードが含まれているため、他のユーザーが読み取れる場所に保存しないでください。 mysql(サーバーを実行するユーザー)としてログインしていない場合は、ファイルにmysqlの読み取りを許可する権限があることを確認してください。

特別な--init-fileオプションを使用してMySQLサーバーを起動します。

mysqld --init-file=/home/me/mysql-init &

サーバーは、--init-fileアカウントのパスワードを変更して、起動時に'root'@'localhost'オプションで指定されたファイルの内容を実行します。

サーバーの通常の起動方法によっては、他のオプションも必要になる場合があります。たとえば、--defaults-filの前に--init-fileeが必要になる場合があります。

サーバーが正常に起動したら、/home/me/mysql-initを削除します。

4
Romeo Ninov

まず、パッケージマネージャーの問題を修正しましょう。以下を使用して、欠落している依存関係をインストールします。

Sudo apt install -f

その後、mysql-serverを既にアンインストールしていた場合は、再インストールしてください:

Sudo apt install mysql-server

まだアンインストールしていない場合、または再インストールしても新しいMySQLルートパスワードの入力が求められない場合は、実行します(必要に応じてバージョン番号を調整します)。

Sudo dpkg-reconfigure mysql-server-5.7

これでパスワードはリセットされました。対話型パスワードプロンプトでrootとして再度ログインしてみてください。

mysql -u root -p

それでもエラーが発生する場合

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

これはおそらく、MySQLサーバーが実行されていないことを意味します。以下で起動してみてください:

Sudo systemctl restart mysql

その後、systemctl status mysqlは「アクティブ(実行中)」として報告する必要があります。その後、再度ログインしてください。

3
Byte Commander