フリーのだいたいWeb屋の技術系メモ
最近AIが好き
仕事じゃないスクリプト書きはとても楽しい

MacでMySQLをインストール

2023年4月14日  2023年4月14日 

在庫管理WebシステムをMac上で作ってみよう

在庫管理システムの有料化とChatGPT4の実装テストを兼ねて、Macで在庫管理Webシステムを作ってみる
出来上がるかどうかは怪しい

MySQLのインストール

まずはMySQLのインストール

MySQLの有無を確認
$ mysql --version

入ってなければ

$ -bash: mysql: command not found

次はbrewのアップデート。少し時間がかかることがある

$ brew update

brewでMySQLをインストール

$ brew install mysql
Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)!
Please create a new installation in /opt/homebrew using one of the
"Alternative Installs" from:
https://docs.brew.sh/Installation
You can migrate your previously installed formula list with:
  brew bundle dump

Errorが出たので書いてある通りにする

Error:ARMプロセッサのHomebrewでは、Intelのデフォルトプレフィックス(/usr/local)にインストールすることができません!
https://docs.brew.sh/Installationの"Alternative Installs"のいずれかを使用して、
/opt/homebrew に新しいインストールを作成してください。
次のコマンドで、以前インストールしたパッケージリストを移行することができます:
brew bundle dump

"Alternative Installs"を読みに行くと、非効率だったり問題が起きたりするけど自己責任でやってね!
問題報告されても問答無用でクローズするよ!
と書いてある
問題が起きたら仕方ないので自力で何とかしましょう

自分でインストールしたパッケージファイルなどを置いておくディレクトリは /opt なので、/opt 以下に homebrew ディレクトリを作って入れます。
"Alternative Installs"に書いてある最初のコマンドではmkdirも書いてあるので/optに移動するだけでインストールできます。
gitからcloneする方法でやってみると

$ sudo git clone https://github.com/Homebrew/brew homebrew
Cloning into 'homebrew'...
remote: Enumerating objects: 236839, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 236839 (delta 4), reused 19 (delta 4), pack-reused 236818
Receiving objects: 100% (236839/236839), 68.90 MiB | 24.83 MiB/s, done.
Resolving deltas: 100% (173584/173584), done.

問題なく終わったので、次はパスを通す。

$ vi ~/.bash_profile

次のように書く

export PATH="/opt/homebrew/bin:$PATH"

保存されているか確認する

$ more ~/.bash_profile

次のようにパスが表示されたらOK!

export PATH="/opt/homebrew/bin:$PATH"

更新を反映させて再度チャレンジ

$ source ~/.bash_profile
$ brew install mysql
Error: /opt/homebrew is not writable. You should change the
ownership and permissions of /opt/homebrew back to your
user account:
  sudo chown -R $(whoami) /opt/homebrew
==> Downloading https://formulae.brew.sh/api/formula.jws.json
######################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################## 100.0%
Error: The following directories are not writable by your user:
/opt/homebrew
/opt/homebrew/bin

You should change the ownership of these directories to your user.
  sudo chown -R $(whoami) /opt/homebrew /opt/homebrew/bin

And make sure that your user has write permission.
  chmod u+w /opt/homebrew /opt/homebrew/bin

/opt/homebrew のユーザが自分じゃなかったので変更する。自分の書き込み権限もついてなかったらつける

$ sudo chown -R $(whoami) /opt/homebrew /opt/homebrew/bin
$ chmod u+w /opt/homebrew /opt/homebrew/bin

再度インストールを行う

$ brew install mysql
(長いので割愛)
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -u root

To restart mysql after an upgrade:
  brew services restart mysql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql/8.0.32: 317 files, 298.2MB
==> Running `brew cleanup mysql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -u root

To restart mysql after an upgrade:
  brew services restart mysql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

無事にインストールできたみたいなので確認

$ mysql --version
mysql  Ver 8.0.32 for macos13.0 on arm64 (Homebrew)

起動はこれでできることを確認

$ brew services restart mysql
(snip)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

停止はこれでできることを確認

$ brew services stop mysql
(snip)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)

今日はここまで

ー記事をシェアするー
B!
タグ
人気の投稿