1
1

More than 3 years have passed since last update.

【2019年版】パーフェクトPHP解説その2 6章 Gitリポジトリ作成〜DB作成

Last updated at Posted at 2019-11-04

IT学習75日目!
前回に続いて、環境構築から始めます。

2 Gitリポジトリ作成

gitはすでにインストールされていますので、バージョンを確認しておきます。

$ git --version
git version 2.14.5

少々古いのですが、今回はこれで良いことにします。

Gitの仕組み、設定やGitHubの登録などについてはこちらの過去記事を参照してください。
ローカルリポジトリ編
リモートリポジトリ編

2.1 ユーザ登録

$ git config --global user.name "perfect-php"
$ git config --global user.email "***@***.com"

次にテキストエディタを指定します。


$ git config --global core.editor 'vim -c "set fenc=utf-8"'

設定内容を確認しておきます


$ less ~/.gitconfig

2.2 ローカルリポジトリの作成

ローカルリポジトリを配置したいディレクトリを作成して移動します。
名前はgit_repositoriesとしておきます。

$ mkdir git_repositories
$ cd git_repositories

次にローカルリポジトリとしたいディレクトリを作成し、移動します。
名前は「p_php_6」とします。

アカウント名:~/environment/git_repositories$ mkdir php_section_6
アカウント名:~/environment/git_repositories$ cd php_section_6

git init してリポジトリを初期化します。

アカウント名:~/environment/git_repositories $ git init
Initialized empty Git repository in /home/ec2-user/environment/git_repositories/p_php_6/.git/

バージョン管理のためのファイル「.git」は隠しファイルとなっているので

アカウント名:~/environment/git_repositories $ ls -a
.  ..  .git

と確認できます。
今後は何か新しく作成、変更をするたびにコミットしていきます。

3 パーフェクトPHP 6章

以下では見出しをパーフェクトPHPの章立てに対応させていきます。
また、書籍の内容では【エラー】になったり、内容を【改善】したりした場合には見出しにタグをつけていきます。

6.2.2 データベースの作成

パーフェクトPHPではいきなりパスワードなくrootユーザーでMySQLを使用しています。
せっかくなので、MySQLのユーザ設定を行います。

6.2.2.1 【改善】mysql_secure_installationによる設定

まずバージョンを確認します。

$ mysql --version

デフォルトでは「mysql Ver 14.14 Distrib 5.5.62」のようです。ここでは最新版への更新は行わないこととします。
MySQLサーバを立ち上げて、起動していることを確認します。

$ sudo service mysqld start
$ sudo lsof -i:mysql

lsofコマンドは、指定したポートを使用しているプロセスを表示できます。
mysqlやHTTPなどのサービス名でウェルノウンポートを調べることもできます。

$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

を実行すると現在のパスワードを求められますが、設定していないのでEnterを押します。

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]

次にrootパスワードを設定するかを求められるので、yとします。

Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 

匿名ユーザを削除するか聞かれるので、yとします。

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 

リモートからのrootユーザでのログインを禁止するかきかれるので、yとします。

... Success!

By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 

デフォルトで使われているtestという名前のサーバを削除するか聞かれるので、yとします。

- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 

最後に今すぐリロードして設定を有効にするか聞かれるので、yとします。

以上で終了です。
パスワードは忘れないようにしましょう。

6.2.2.2 【エラー】CREATE DATABASE

先ほど設定したパスワードでrootアカウントにログインします。

$ mysql -u root -p

下記のコマンドからデータベースを作成し、クライアントを閉じます。
DB名は「oneline_bbs」です。
どうやら1行掲示板を作るようです。

mysql> CREATE DATABASE 'oneline_bbs' DEFAULT CHARACTER SET utf8;
mysql> quit

syntax errorになりました。

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''oneline_bbs' DEFAULT CHARACTER SET utf8' at line 1

調べてみるとどうやらデータベース名に''が不要なようでした。

mysql> CREATE DATABASE oneline_bbs DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

成功しました。

6.2.3 【エラー】テーブルの作成

postテーブルを作成します。
次の4つのカラムを定義します。

  • id: 数値の連番(主キー)
  • name: 投稿者名
  • comment: コメント
  • created_at: 投稿日時

※ railsではupdated_at というカラムも自動的に作成されていた気がしますが、今回は本文に従います。

MySQLクライアントでoneline_bbsデータベースを開きます。

$ mysql -u root -p oneline_bbs
mysql>

下記のSQL文を入力します。

CREATE TABLE 'post' (
  'id' INTEGER NOT NULL AUTO_INCREMENT, # 整数、nullを認めない、自動的に1増える設定
  'name' VARCHAR(40), # 最大40文字までの可変長文字列
  'comment' VARCHAR(200), # コメントは200文字まで
  'creatred_at' DATETIME,
  PRIMARY KEY(id) # 主キーをidに設定
) ENGINE = INNODB; # データベースエンジンをイノデービーに設定

またもやsyntax errorになりました。
きっとシングルクォーテーションが不要なので、それで挑戦

CREATE TABLE post (
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(40),
  comment VARCHAR(200),
  creatred_at DATETIME,
  PRIMARY KEY (id) 
) ENGINE = INNODB;
Query OK, 0 rows affected (0.00 sec)

予想通りうまくいきました。
MySQL文では' 'は不要のようです。

正しく追加できたか確認します。

mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oneline_bbs |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> SHOW TABLES;
+-----------------------+
| Tables_in_oneline_bbs |
+-----------------------+
| post |
+-----------------------+
1 row in set (0.00 sec)

mysql> show columns from post;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(40) | YES | | NULL | |
| comment | varchar(200) | YES | | NULL | |
| creatred_at | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> exit

確かに作成できています。
続きは次回へ

1
1
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
1
1