質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

解決済

1回答

1796閲覧

mysqlでmysql -u root してもエラーになってしまうので解決したい

senseIY

総合スコア281

0グッド

0クリップ

投稿2022/02/01 09:00

編集2022/02/01 12:10

解決したいこと

mysqlでのエラーが解決できないので解決したい

発生している問題・エラー

#1つめ ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111) #2つめ Starting MySQL ./home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 1: eval: cannot create /var/log/mysql/error.log: Permission denied /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied * The server quit without updating PID file (/var/run/mysqld/mysqld.pid).

自分で試したこと

mysql -u rootと入力すると

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)

というエラーが発生

mysqlのサーバーが動いていないのではないかと考えたが、

➜ dictation_app git:(master) ✗ mysql.server start Starting MySQL * ➜ dictation_app git:(master) ✗ /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied cat: /var/run/mysqld/mysqld.pid: 許可がありません rm: '/var/run/mysqld/mysqld.pid' を削除できません: 許可がありません 2022-02-01T05:41:01.994696Z mysqld_safe Fatal error: Can't remove the pid file: /var/run/mysqld/mysqld.pid. Please remove the file manually and start /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe again; mysqld daemon not started /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied

のようなエラーになってしまった。エラーの指示通りに権限を与えると

➜ dictation_app git:(master) ✗ /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied rm: '/var/run/mysqld/mysqld.pid' を削除できません: 許可がありません 2022-02-01T05:54:09.215517Z mysqld_safe Fatal error: Can't remove the pid file: /var/run/mysqld/mysqld.pid. Please remove the file manually and start /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe again; mysqld daemon not started /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied

のようにcat: /var/run/mysqld/mysqld.pid: 許可がありませんが消えただけ。手動でファイルを消すように指示されたので/var/run/mysqld/mysqld.pid.
を消してもう一度mysql.server start

➜ dictation_app git:(master) ✗ mysql.server start Starting MySQL ./home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 1: eval: cannot create /var/log/mysql/error.log: Permission denied /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe: 144: cannot create /var/log/mysql/error.log: Permission denied * The server quit without updating PID file (/var/run/mysqld/mysqld.pid).

ここで一旦mysqlが動いているか確認するため

➜ dictation_app git:(master) ✗ sudo systemctl enable --now mysql Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable mysql

もう一度やっても上手くいかず。

➜ dictation_app git:(master) ✗ mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)

また今の状態は

➜ dictation_app git:(master) ✗ systemctl status mysql.service ● mysql.service - MySQL Community Server Loaded: loaded (8;;file://sense-iy-CFSZ5-3/lib/systemd/system/mysql.s> Active: active (running) since Tue 2022-02-01 14:21:55 JST; 2h 17min > Docs: 8;;man:mysqld(8)^Gman:mysqld(8)8;;^G 8;;http://dev.mysql.com/doc/refman/en/using-systemd.html^Ghtt> Main PID: 23590 (mysqld) Status: "Server is operational" Tasks: 38 (limit: 9330) Memory: 343.5M CGroup: /system.slice/mysql.service └─23590 /usr/sbin/mysqld 2月 01 14:21:54 sense-iy-CFSZ5-3 systemd[1]: Starting MySQL Community Ser> 2月 01 14:21:55 sense-iy-CFSZ5-3 systemd[1]: Started MySQL Community Serv> ➜ dictation_app git:(master) ✗ ps aux | grep mysql mysql 23590 0.3 4.7 2269384 383224 ? Ssl 14:21 0:35 /usr/sbin/mysqld sense_iy 29465 0.0 0.0 18720 2464 pts/0 S+ 16:50 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox mysql

ここで、他のサイトを参考にもう一度pidファイルを作ることにした

sudo touch sense-iy-CFSZ5-3.pid

その後権利変更

#なぜか参考にしたサイトに書かれていた通りにやるとエラーに ➜ mysqld sudo chown -R _mysql:_mysql var/run/mysql [sudo] sense_iy のパスワード: chown: ユーザー指定が不正: `_mysql:_mysql' #アンダーバー消したらうまくいったけど合っているか不明。なぜこのコマンドをして権利変更するのかもよくわからないけどとりあえず実行してみた。もともとrootだった部分がmysqlになった。 ➜ mysqld sudo chown -R mysql:mysql /var/run/mysqld ➜ mysqld ls -l sense-iy-CFSZ5-3.pid -rw-r--r-- 1 mysql mysql 0 2月 1 17:02 sense-iy-CFSZ5-3.pid

しかし変化なし。そもそも指示にしたがって消したのにもう一度作るのは少しおかしいのではと考えてその後いろいろなサイトを見るも、mariaDB(1度もインストールしてないです)関連の記事が多くどれが有効なのか分からず、ここで詰まって進めません。

###その他

・ubuntu20.04を使っています。
・再インストールと再起動は試しました。
・sqlite3を使っていたときはうまく行きました。現在はアンインストールしています。
・もともとsqlite3を使っている途中のディレクトリででmysqlに変えたためそれが原因かと思ったので、0から作り直しました。なのでsqlite3は関係ないと思います。
・ubuntuの記事があまりなかったので情報を集めるのが難しく、とりあえず別のOSの記事も参考にしています。
・mysqlは最新です。
・なぜかsudo mysql.server start をすると
sudo: mysql.server: コマンドが見つかりません
となってしまいます。
・mysql自体は起動しているようです。
・入門者なので変な操作をしているかもしれません。

何かアドバイスがあれば教えていただきたいです。よろしくお願い致します。

追記

エラーに

Please remove the file manually and start /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe again;

とあったので実行。しかしこのコマンドで検索しても2件しかヒットしなかったのでどんなコマンドか分からないけど、とりあえずやってみた。

➜ dictation_app git:(master) ✗ sudo /home/linuxbrew/.linuxbrew/Cellar/mysql/8.0.28/bin/mysqld_safe 2022-02-01T10:46:07.812058Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2022-02-01T10:46:07.847246Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 2022-02-01T10:47:48.265914Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

変化なしでした。

さらに追記

現在my.cnfが間違っているのではないかと思ったので調査中です。一応3パターン試してみたのですが、どれもうまく行きませんでした。

1つ目 (初期状態)

# Copyright (c) 2015, 2021, Oracle and/or its affiliates. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License, version 2.0, # as published by the Free Software Foundation. # # This program is also distributed with certain software (including # but not limited to OpenSSL) that is licensed under separate terms, # as designated in a particular file or component or in included license # documentation. The authors of MySQL hereby grant you an additional # permission to link the program and your derivative works with the # separately licensed software that they have included with MySQL. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License, version 2.0, for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/

2つ目

[mysqld] socket=/tmp/mysql.sock [client] soket=/tmp/mysql.sock

3つ目

[vagrant@asianux7 ~]$ sudo vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 character-set-server=utf8 #default_password_lifetime = 0 ←**ここを削除** [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/mysql/mysql.cnf.d

#それぞれのエラー
上から順に初期、2つ目、3つ目

➜ dictation_app git:(master) ✗ mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111) ➜ dictation_app git:(master) ✗ mysql -u root mysql: [ERROR] unknown variable 'soket=/tmp/mysql.sock'. ➜ dictation_app git:(master) ✗ mysql -u root mysql: Can't read dir of '/etc/mysql/mysql.cnf.d' (OS errno 2 - No such file or directory) mysql: [ERROR] Stopped processing the 'includedir' directive in file /etc/mysql/my.cnf at line 48. mysql: [ERROR] Fatal error in defaults handling. Program aborted!

また、2つの参考にさせて頂いたサイトの文脈的にもともとの初期状態で

[mysqld] socket=/var/lib/mysql/mysql.sock

のように記述されているらしいのですが自分のにはありませんでした。開いている場所が違うのでしょうか?パスは/etc/my.cnfです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

いったい何をしたのかよくわかりませんが、linuxbrewを使っているなら
brew install mysql
brew services start mysql
の2行で終わりです。

投稿2022/02/01 14:09

technocore

総合スコア7200

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

senseIY

2022/02/02 10:45

なんとかエラーを解決できました。どうやらbrewでmysqlをインストールしているにもかかわらず、別の方法でもう一度mysqlをインストールしていたのが問題だったようです(おそらく2つインストールしていた?)。brew以外のmysqlをすべて削除したらうまく動きました。最初の頃にmysqlをインストール→設定を適当にコピペ→分からなくなり別のやり方で再インストールしていた時期があったため、brewでインストールしていたのを忘れていました。今回は貴重なお時間を割いていただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問