20190829のMySQLに関する記事は4件です。

macOS に Homebrew で MySQL 8.0 をインストールしてデータベースを作成する

Homebrew で MySQL をインストール

$ 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 -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
?  /usr/local/Cellar/mysql/8.0.17: 284 files, 272.5MB

MySQL サーバを起動

brew services start mysql で MySQL サーバを起動する。

$ brew services start mysql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
(中略)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

MySQL の初期設定を実施

mysql_secure_installation で対話型の初期設定を開始する。

$ mysql_secure_installation

いくつかの設定について尋ねてくるのでそれに答えていく。
MySQL root ユーザーのパスワードもここで設定できる。

データベースを作成

mysql コマンドを実行する。

$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.17 Homebrew

Copyright (c) 2000, 2019, 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.

CREATE DATABASE でデータベースを作成する。
ここでは testdb という名前のデータベースを作成する。

mysql> CREATE DATABASE IF NOT EXISTS testdb
    -> DEFAULT CHARACTER SET utf8mb4
    -> DEFAULT COLLATE utf8mb4_general_ci
    -> ;
Query OK, 1 row affected (0.01 sec)

ユーザーを作成

CREATE USER でユーザーを作成する。
ここではユーザー名に java、パスワードに cafebabe を指定する。

mysql> CREATE USER 'java'@'localhost' IDENTIFIED BY 'cafebabe';
Query OK, 0 rows affected (0.04 sec)

ユーザーに権限を設定する。

mysql> GRANT CREATE, DROP, SELECT, INSERT, UPDATE, DELETE ON testdb.* TO 'java'@'localhost';
Query OK, 0 rows affected (0.01 sec)

参考資料

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Qiitaクローン】1.プロジェクトの開始

はじめに

未経験からWebエンジニア(サーバサイド)へ転職するため勉強中です。
Qiitaのクローンを一から作成しながら、こちらに投稿することでより理解を深めていけたらなと思います。

※ cloud9にて開発していきます。
Rails 5.2.3
Ruby 2.5.3 [x86_64-linux]

機能一覧

  • ユーザ登録/ログイン認証機能
  • 記事の投稿/一覧機能
  • 記事へのコメント機能
  • フォロー機能
  • いいね機能
  • 検索機能
  • 通知機能

今の所これくらいで必要があれば随時追加していきます。

プロジェクト開始

$ rails new qiita_clone --database=mysql

$ rails new でRailsプロジェクトを立ち上げます。データベースはMySQLを使います。

$ rails db:create

qiita_clone_development データベースを作成します。

$ rails s

サーバを起動し、“Yay! You’re on Rails!” の表示を確認。


初日なのでここまで!

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

rails6/windows10/ubuntu/mySQL

windows10でruby on rails使えるようにします。

まずubuntuを入れます。(省略)
rbenvでRubyをインストールします。(省略)
node.jsとかBundlerを入れます。(省略)

# mkdir rails_app
# cd rails_app
# bundle init

Gemfileが出来るので編集

\# gem "rails"

の行のコメントアウトを外す

# bundle install --path vendor/bundle
# bundle exec rails new . -B -d mysql --skip-turbolinks --skip-test

→ERROR
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.が出る

MYSQLを導入します。
https://dev.mysql.com/downloads/repo/apt/
で、パッケージをダウンロードします。
下のコマンドでインストールします。
インストール中にバージョン選択を求められます。

# sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb
# sudo apt-get install mysql-server
# sudo apt-get install libmysqld-dev

MySQLを起動します。

# sudo /etc/init.d/mysql start

bundle installした後、DBを作成。

# bundle exec rake db:create
RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment
Access denied for user 'root'@'localhost'
Couldn't create 'rails_app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error: Access denied for user 'root'@'localhost'
              :

こんなのがでるので

# bundle
# bundle exec rails webpacker:install

もう一度bundle exec rake db:create

warning Skipping preferred cache folder "/home/user/.cache/yarn" because it is not writable.
warning Selected the next writable cache folder in the list, will be "/tmp/.yarn-cache-1000".
Access denied for user 'root'@'localhost'
Couldn't create 'rails_app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error: Access denied for user 'root'@'localhost'
                  :

権限がないので、ユーザー作って権限を与えます

sql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
sql> GRANT ALL ON rails_app_development.* TO 'user'@'localhost';

config/database.ymlの設定と合わます

rails_app_testでも権限ないとでるようなので、
もう一回権限付与します。するとbundle exec rake db:create出来ました

# bin/rails s

でサーバーがちゃんと起動したので問題ないはず

http://localhost:3000/
に繋ぎます

FireShot Capture 059 - Ruby on Rails - localhost.png

おわり
いろいろ試しましたが、上の手順で良さそうです
嵌りに嵌りました

ubuntu version18.04
rbenv 2.6.3
Rails 6.0.0
10.1.41-MariaDB

その他
MySQLが起動しない

# sudo /etc/init.d/mysql start

→ERROR

下を実行

# sudo apt clean
# sudo apt install mariadb-common mariadb-server mariadb-client

→ mysql -u root する
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
こんなのがでる

→ sudo mysql -u root だと入れた
Ubuntu特有の初期設定らしい

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

rails6サーバー起動まで手順

windows10でruby on rails使えるようにします。

まずubuntuを入れます。(省略)
rbenvでRubyをインストールします。(省略)
node.jsとかBundlerを入れます。(省略)

$ mkdir rails_app
$ cd rails_app
$ bundle init

Gemfileが出来るので編集
# gem "rails"
の行のコメントアウトを外す

$ bundle install --path vendor/bundle
$ bundle exec rails new . -B -d mysql --skip-turbolinks --skip-test

→ERROR
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.が出る

MYSQLを導入します。
https://dev.mysql.com/downloads/repo/apt/
で、パッケージをダウンロードします。
下のコマンドでインストールします。
インストール中にバージョン選択を求められます。

$ sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb
$ sudo apt-get install mysql-server
$ sudo apt-get install libmysqld-dev

MySQLを起動します。

$ sudo /etc/init.d/mysql start

bundle installした後、DBを作成。

$ bundle exec rake db:create
RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment
Access denied for user 'root'@'localhost'
Couldn't create 'rails_app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error: Access denied for user 'root'@'localhost'
              :

こんなのがでるので

$ bundle
$ bundle exec rails webpacker:install

もう一度bundle exec rake db:create

warning Skipping preferred cache folder "/home/user/.cache/yarn" because it is not writable.
warning Selected the next writable cache folder in the list, will be "/tmp/.yarn-cache-1000".
Access denied for user 'root'@'localhost'
Couldn't create 'rails_app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error: Access denied for user 'root'@'localhost'
                  :

権限がないので、ユーザー作って権限を与えます

sql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
sql> GRANT ALL ON rails_app_development.* TO 'user'@'localhost';

config/database.ymlの設定と合わます

rails_app_testでも権限ないとでるようなので、
もう一回権限付与します。するとbundle exec rake db:create出来ました

$ bin/rails s

でサーバーがちゃんと起動したので問題ないはず

http://localhost:3000/
に繋ぎます

FireShot Capture 059 - Ruby on Rails - localhost.png

おわり
いろいろ試しましたが、上の手順で良さそうです
嵌りに嵌りました

ubuntu version18.04
rbenv 2.6.3
Rails 6.0.0
10.1.41-MariaDB

-追記-
postgresqlだったらすんなり行けました。
はぁ・・・

その他

UbuntuでCドライブ配下に移動

$ cd /mnt/c

MySQLが起動しない

$ sudo /etc/init.d/mysql start

→ERROR

下を実行

$ sudo apt clean
$ sudo apt install mariadb-common mariadb-server mariadb-client

mysql -u root する ERROR 1698 (28000): Access denied for user 'root'@'localhost' こんなのがでる

→ sudo mysql -u root だと入れた
Ubuntu特有の初期設定らしい

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む