tail -f /dev/null

If you haven't had any obstacles lately, you're not challenging. be the worst.

MySQLへ接続ができない&権限エラーの対処 Macports

Environment

Phenomenon

MySQLへ接続しようとしても、SocketがあるのにSocketがないと怒られる

// 接続を試みる
% mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql56/mysqld.sock' (38)


// MySQLAdminから接続を試みる
/opt/local/lib/mysql56/bin/mysqladmin -u root password 'root'
/opt/local/lib/mysql56/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/opt/local/var/run/mysql56/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/opt/local/var/run/mysql56/mysqld.sock' exists!


// 読み込み先を再度設定してみる
% sudo port select --set mysql mysql56
Selecting 'mysql56' for 'mysql' succeeded. 'mysql56' is now active.


// 初期化を試みる
% sudo -u _mysql /opt/local/lib/mysql56/bin/mysql_install_db
Installing MySQL system tables...2015-06-06 07:17:08 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-06-06 07:17:08 0 [Note] /opt/local/lib/mysql56/bin/mysqld (mysqld 5.6.24) starting as process 98862 ...
2015-06-06 07:17:08 98862 [Note] InnoDB: Using atomics to ref count buffer pool pages
2015-06-06 07:17:08 98862 [Note] InnoDB: The InnoDB memory heap is disabled

[省略]

2015-06-06 07:17:11 98863 [Note] InnoDB: Shutdown completed; log sequence number 6372499
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  /opt/local/lib/mysql56/bin/mysqladmin -u root password 'new-password'
  /opt/local/lib/mysql56/bin/mysqladmin -u root -h interest-marketing-no-MacBook-Air.local password 'new-password'

Alternatively you can run:

  /opt/local/lib/mysql56/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd /opt/local ; /opt/local/lib/mysql56/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file /opt/local/etc/mysql56/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /opt/local/etc/mysql56/my-new.cnf,
please compare it with your file and take the changes you need.



// localディレクトリ配下で、Deamonをスタートさせる
% cd /opt/local

% sudo /opt/local/lib/mysql56/bin/mysqld_safe &
[1] 98958


// MySQLのプロセスを確認、サーバーは立っていない
% ps -ax | grep mysql
231:96358 ttys000    0:00.00 grep --color -n -I --exclude=*.svn-* --exclude=entries --exclude=*/cache/* mysql


// MySQL Serverを再起動(portが2週間更新されていない)
% sudo port load mysql56-server
Warning: port definitions are more than two weeks old, consider updating them by running 'port selfupdate'.


// とりあえずportを更新(権限エラーが)
% sudo port selfupdate
% sudo port upgrade outdated

[省略]
--->  Verifying checksums for mysql56
can't create directory "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_databases_mysql56": permission denied


// ひとまず重複しているパッケージをアンインストール
% sudo port -uf uninstall


// buildディレクトリ配下を権限変更
% sudo chmod -R 775 /opt/local/var/macports/build/


// mysqlユーザーで初期化を試みる(mysqldの`default-character-set=utf8`がよくないらしい)
% sudo /opt/local/lib/mysql56/bin/mysql_install_db --user _mysql

[省略]
2015-06-06 07:28:29 0 [Note] /opt/local/lib/mysql56/bin/mysqld (mysqld 5.6.24) starting as process 99776 ...
2015-06-06 07:28:29 99776 [ERROR] /opt/local/lib/mysql56/bin/mysqld: unknown variable 'default-character-set=utf8'
2015-06-06 07:28:30 99776 [Note] /opt/local/lib/mysql56/bin/mysqld: Shutdown complete


// my.cnf を編集(`default-character-set`部分をコメントアウト)
% vi /opt/local/etc/mysql56/my.cnf

==

# Use default MacPorts settings
# !include /opt/local/etc/mysql56/macports-default.cnf

[mysqld]
port=3306
key_buffer_size=16M
max_allowed_packet=16M
# default-character-set = utf8
# socket = /opt/local/var/run/mysql56/mysqld.sock

[client]
port=3306
# default-character-set = utf8
# socket = /opt/local/var/run/mysql56/mysqld.sock

==


// MySQLサーバーを再起動
% sudo port load mysql56-server


// Apacheを再起動
% sudo /opt/local/apache2/bin/apachectl -k restart


// 入れた
% mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.24 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

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>


// ソケットは無事通過するようになったが、rootだと権限エラーが表示されてしまう
% mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

権限エラーが表示されてしまう

// rootだとエラーが表示されるので
% mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)


// 権限スキップでMySQLサーバーをスタートさせる
% sudo launchctl start org.macports.mysql56 --skip-grant-tables


// 無事接続完了
% mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.24 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

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>

Some of factor of error

  • MySQLサーバが起動していない
    • % sudo port load mysql56-server
  • MySQLの設定ファイルがしっかりと設定されていない
    • % sudo vi /opt/local/etc/mysql56/my.cnf
  • MySQLのSocketがない(MySQLがSocketを作成する権限があるかも併せて)
    • % sudo chmod 755 /opt/local/var/run/mysql56/
    • % sudo touch /opt/local/var/run/mysql56/mysqld.sock