MySQL は, オープンソースのリレーショナルデータベース管理システムである. MySQL のインストールとテスト実行を行なう.
【目次】
Ubuntu で OS のシステム更新を行うときは, 次のコマンドを実行.
Ubuntu のインストールは別ページ »で説明
sudo apt -y update sudo apt -yV upgrade sudo /sbin/shutdown -r now
インストールするには, 次のコマンドを実行.
sudo apt -y update sudo apt -y install mysql-client mysql-server libcppdb-odbc0 python3-mysqldb
エラーメッセージが出なければ OK.
sudo systemctl restart mysql systemctl status mysql
sudo cat /etc/passwd | grep mysql
※ 「x」は no password という意味(パスワードがないという意味ではない)
サービスアカウントは Linux が管理するアカウントのこと.MySQL が管理するアカウントとは別のものである.
/var/lib/mysql になる.
sudo ls -la /var/lib/mysql
sudo mysql_secure_installation
「y」, Enter キー
HOGE$#34hoge5 のように設定する.
パスワードは,必ず独自に設定してください.
「y」, Enter キー
「y」, Enter キー
「y」, Enter キー
「y」, Enter キー
「y」, Enter キー
「y」, Enter キー
Ubuntu のアカウント root の言言で「mysql」を実行.
「status」により,MySQL root ユーザで使用していること,そして 使用中のデータベースが無いことを確認.
sudo mysql status show databases; exit
MySQL の auth_socket プラグインが使用されている. そのため,Ubuntu の root ユーザは, MySQL の root ユーザにログインしている. このときパスワードが不要であるのは正常動作である.
mysqladmin -u root -p version
sudo systemctl restart mysql systemctl status mysql
sudo systemctl stop mysql systemctl status mysql
パスワードは,英文字,数字,記号を使う.日本語は使わない.
パスワードを新規に設定する場合は,次のように操作.
mysqladmin --user=root password "(新しいパスワード)"
すでにパスワードを設定済みで,変更をしたい場合は,次のように操作.
mysqladmin --user=root --password=(古いパスワード) password "(新しいパスワード)"
「root」は MySQL データベース管理者のユーザ名,
「-D mysql」は,使用するデータベースを mysql に設定している.
sudo mysql -D mysql
create table order_records ( id integer primary key not null, year integer not null CHECK ( year > 2008 ), month integer not null CHECK ( month >= 1 AND month <= 12 ), day integer not null CHECK ( day >= 1 AND day <= 31 ), customer_name text not null, product_name text not null, unit_price real not null check ( unit_price > 0 ), qty integer not null default 1 check ( qty > 0 ), created_at timestamp not null default current_timestamp, updated_at timestamp not null default current_timestamp on update current_timestamp, check ( ( unit_price * qty ) < 200000 ) );
start transaction; insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2020, 7, 26, 'kaneko', 'orange A', 1.2, 10 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2020, 7, 26, 'miyamoto', 'Apple M', 2.5, 2 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2020, 7, 27, 'kaneko', 'orange B', 1.2, 8 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2020, 7, 28, 'miyamoto', 'Apple L', 3 ); commit;
select * from order_records;
start transaction; update order_records set unit_price = 11.2 where id = 1; commit; select * from order_records;
show tables;
exit