0
0

More than 1 year has passed since last update.

MySQLでパスワードなしでログイン

Posted at

テストしたいだけなのに、MySQLでパスワードを求められたりして大変だったので、パスワードなしでログインできるようにするためのメモ。

環境
Amazon Web Service Lightsail
AmazonLinux2

インストールされていることを確認。

mysql --version

Macなどで、MySQLをインストールした場合は、よくmysql.server startコマンドで起動するようですが、私の環境ではmysqldをインストールしたので、systemdで起動します。

$ sudo systemctl start mysqld.service

起動確認

$ sudo systemctl status mysqld.service

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-09-23 07:54:39 UTC; 18h ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 6219 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=e
xited, status=0/SUCCESS)
  Process: 6169 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 6223 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─6223 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Sep 23 07:54:33 ip-172-26-12-127.ap-northeast-1.compute.internal systemd[1]: Starting MySQL Server...
Sep 23 07:54:39 ip-172-26-12-127.ap-northeast-1.compute.internal systemd[1]: Started MySQL Server.

activeになっていることが確認ができたので、初期パスワードを確認していきます。

$ sudo vi /var/log/mysqld.log

viのコマンドモードでログファイルが開かれるので、
/passwordと入力して
パスワードを検索します。

[Note] A temporary password is generated for root@localhost: jiY9><-Y)/w:

初期設定のパスワードがjiY9><-Y)/w:だと分かったので、コピーしておいてMySQLの初期設定を行います。

$ sudo mysql_secure_installation

新しいパスワードを入力していきます。

Enter password for user root: jiY9><-Y)/w:
New password: Pa66wo+d
Re-enter new password: Pa66wo+d
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :y
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :y

rootをパスワードなしでログインできるようにする

$ sudo vi /etc/my.cnf

最終行あたりにskip-grant-tablesを追加して、パスワードなしでログインできるようにします。

[mysqld]
.
.
skip-grant-tables
.
.

MySQLを再起動

$ sudo systemctl restart mysqld.service

パスワードなしでログインできることを確認

$ mysql -u root

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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

参考

https://weblabo.oscasierra.net/mysql-57-init-setup/
https://qiita.com/ksugawara61/items/d8d69b7d57a3afcef980

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0