MySQLのセキュリティ設定している人はここは飛ばす
—
$マークはMacのターミナルを使用している。
ターミナルは、アプリの「その他」の中にある。(何故かわかりにくいところに入れてある)
mysqlサーバを起動する
$ mysql.server start
Starting MySQL
. SUCCESS!
止めるときは
$ mysql.server stop
次にセキュリティ設定
このサイトを参考にすると良い
https://style.potepan.com/articles/19020.html
MySQLのセキュリティ設定
>>>>>>>>>>>>>>>>>>>以下入力した内容 ここから↓
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
$ mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 >>0:LOWとした。本番環境なら2だ Please set the password for root here. New password: >>パスワードを入れる Re-enter new password: >>パスワードの確認 Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y 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? (Press y|Y for Yes, any other key for No) : y Success. 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y Success. All done! |
>>>>>>>>>>>>>>>>>>>>ここまで
mysqlにRootでログインする。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ mysql -u root -p Enter password: >>先程登録したパスワード Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.22 Homebrew Copyright (c) 2000, 2020, 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で抜ける
mysql> exit
Bye
ユーザの作成・削除
例
user:maseda
pass:Test1030 >>ここのパスワードは知られても気にしない。このパスワードを他で使わないから。
ユーザの作成
まずは
rootでログインする
mysql -u root -p
(pass)
以下はmysqlコマンドとして入力する。masedaユーザを作成
一人でしか使わないが、今後誰かと共同作業があるかもしれないので勉強のために
ユーザを作る。
create user ‘maseda’@localhost identified by ‘Test1030’;
作成したユーザの確認
select user, host from mysql.user;
1 2 3 4 5 6 7 8 9 10 11 |
mysql> select user, host from mysql.user; +------------------+-----------+ | user | host | +------------------+-----------+ | maseda | localhost | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 5 rows in set (0.00 sec) |
MySQLの設定をしている人はここから作業をする。
root権限でユーザにグローバルレベルでcreate権限を与える。すべてのDBで使える。
制限するときは、DB名を指定するが今はしない。
権限を与える。必要なときに必要な分を与える
grant create on *.* to maseda@localhost;
GRANT SELECT,UPDATE,INSERT,DELETE ON *.* to maseda@localhost;
GRANT DROP,CREATE VIEW,ALTER,EXECUTE,INDEX,SHOW DATABASES,SHOW VIEW,FILE ON *.* to maseda@localhost;
exitでrootを抜ける。
→次はここから、2020/12/15
ユーザでログイン
mysql -u maseda -p
psssはTes*****にした。上に記載。このパスワードはデモ用なので知られてOK
次にデータベースを作成して、テーブルを作る。
そしてCSVファイルを読み込む
自動採番の例 auto_increment
>>create table staff(id int auto_increment, name varchar(10), index(id));
今回作成するDB(Stock)とテーブル(Table_StockOption)を作成
株探のオプション記事の数値をDB化する。
作成の例
DB:Stock
Table:Table_StockOption
・カラム
ID:id , int auto_increment、自動採番
出来高1 Volume1
前日比1 Change1
価格1 Price1
行使価格 ExercisePrice
価格2 Price2
前日比2 Change2
出来高2 Volume2
月限 Month , 例 12月限 12
取引日付、株探掲載日付 OptionDate ,date , DATE(NOW()) >> 2014-01-15
テーブル取り込み作成日 CreateDate ,datetime, NOW() >>2014-01-15 17:05:43
予備 (テキスト),text
//正負を表す ‘-‘ や ‘+’ を記述することができる。+10の+記号はINT型で可能
挿入時は+記号はない。10となり、マイナスは-10となる
NULLは処理速度が遅くなるのでできるだけ使わない。空にしたくないなら-1とか’unknown’とか入れる
大量のデータがないときはそれほど考慮しなくてよいだろう。
・ユーザはmaseda
・DBを作成
CREATE DATABASE Stock DEFAULT CHARACTER SET utf8mb4;
・DBを指定する
use Stock;
・テーブルを作成
CREATE TABLE Table_StockOption(
ID int(11) NOT NULL AUTO_INCREMENT,
Volume1 int(11),
Change1 int(11),
Price1 int(11),
ExercisePrice int(11) NOT NULL,
Price2 int(11),
Change2 int(11),
Volume2 int(11),
Month int(2),
OptionDate DATE,
CreateDate DATETIME,
YOBI text,
PRIMARY KEY (ID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
>>PRIMARY KEY(ID)が必要。ないとエラーになる。
1 2 3 4 5 6 |
mysql> show tables; +-------------------+ | Tables_in_stock | +-------------------+ | Table_StockOption | +-------------------+ |
mysql> show columns from Table_StockOption;
ERROR 1142 (42000): SELECT command denied to user ‘maseda’@’localhost’ for table ‘table_stockoption’
mysql>
この場合権限がないので、権限を与える。
GRANT SHOW DATABASES ON *.* to maseda@localhost;
これでいいはず。もしこれでだめな場合は、必要な権限を与えて。
以下はカラム一覧を表示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
mysql> show columns from Table_StockOption; +---------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+----------+------+-----+---------+----------------+ | ID | int | NO | PRI | NULL | auto_increment | | Volume1 | int | YES | | NULL | | | Change1 | int | YES | | NULL | | | Price1 | int | YES | | NULL | | | ExercisePrice | int | NO | | NULL | | | Price2 | int | YES | | NULL | | | Change2 | int | YES | | NULL | | | Volume2 | int | YES | | NULL | | | Month | int | YES | | NULL | | | OptionDate | date | YES | | NULL | | | CreateDate | datetime | YES | | NULL | | | YOBI | text | YES | | NULL | | +---------------+----------+------+-----+---------+----------------+ 12 rows in set (0.01 sec) |
これでテーブルの作成まで終わった。
次は、株探からコピペしたテキストをCSVファイルに置き換えて
mysqlに取り込む作業。
まずは、PythonでCSVファイルに変更するスクリプトを書く