LoginSignup
28
29

More than 5 years have passed since last update.

MySQLをMacに導入して基礎を学ぶ

Last updated at Posted at 2015-09-10

公開してみたいHPを作成するために勉強していきます。
とりあえずの目標はwordpressで自分のノウハウを貯める記事を作成するところからでしょうか

まずはMySQLをMacに導入してMySQLの基礎から勉強していきます。
なお参考にしたのはドットインストールさんです

環境

  • Mac OS X 10.10.2(Yosemite)

インストール

Homebrewでインストールします

$ brew update
$ brew install mysql

ここまではできていました。

インストール出来たら

$ brew info mysql

で情報の確認

私のバージョンは5.6.26でした

起動

rootユーザーで起動します
なお-u ユーザ名でrootユーザ指定をしています。
rootユーザは全てのデータベースにアクセスする事ができる。

mysql -u root

しかしエラー
これはローカルのサーバーで起動したことないことによるものでした。

$ brew info mysql

によると

To connect:
    mysql -uroot

To have launchd start mysql at login:
  ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
  mysql.server start

なおlaunchdは起動・停止・管理をするものです。
今回はlaunchdなしで使うとして、

$ mysql.server start

を実行

Starting MySQL
 SUCCESS!

と成功を表す文字が…!
もう一度

mysql -u root

とすると

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.24 Homebrew

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>

と歓迎を表されると共にコマンド入力ができるようになりました。一度exitと打って停止します。

パスワードの設定

以下を実行してpasswordを設定します

set password for root@localhost=password('hogehoge');

成功したらQuery OK

とでます。一度停止して

mysql -u root

を実行してログインしようとしてもエラー

mysql -u root -p

でパスワードを入力してログインできるようになります。

データベースの作成

以下はmysqlが起動している状態で行います

  • データベースの作成
create database test_app;
  • データベース一覧の取得
show databases;
  • データベースの削除
drop databases test_app;
  • データベースの切り替え
use test_app;

作業ユーザの作成

データベースはrootユーザで作業せずに作業ユーザを作るのが一般的

まずデータベースを作成(rootユーザのみ)
そして以下を実行

grant all on test_app.* to dbuser@localhost identified by 'password_hoge';

grant all on    (許可)
.*    (全て)
identified     (特定できる)
dbuser@localhost (ユーザ@サーバ)

こういった一般的なものは覚えておきたいです…

exit;で停止して

$ mysql -u dbuser -p test_app

を実行して、作成したユーザで入れているのが確認できます

28
29
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
28
29