This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

【CakePHP速習会】DBを準備する

Last updated at Posted at 2016-07-03

今回のチュートリアルで使用するDBを作成します。

1. ターミナルを起動する

mac

cd ./dip-vagrantfile
vagrant ssh

Windows

TeraTermを起動する。

ホスト:192.168.33.10
image

ユーザ名/パスフレーズ:vagrant/vagrant
image

2. DBに接続する

mysql -u root

3. データベースを作成する

create database blog_test character set  utf8;
use blog_test

4. テーブルを作成する

CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

5. データを登録する

INSERT INTO posts (title,body,created) VALUES ('タイトル', 'これは、記事の本文です。', NOW());
INSERT INTO posts (title,body,created) VALUES ('またタイトル', 'そこに本文が続きます。', NOW());
INSERT INTO posts (title,body,created) VALUES ('タイトルの逆襲', 'こりゃ本当にわくわくする!うそ。', NOW());

6. 登録したデータを確認する

SELECT * FROM posts;

7. DBを切断する

\q

8. CakePHPから接続できるようにする

8-1. 以下のファイルをリネームする

  /cakephp/app/Config/database.default.php
  ↓
  /cakephp/app/Config/database.php

8-2. database.phpを編集する

/cakephp/app/Config/database.php
    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root', // 変更する
        'password' => '', // 変更する
        'database' => 'blog_test', // 変更する
        'prefix' => '',
        //'encoding' => 'utf8',
    );
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