ラベル mysql の投稿を表示しています。 すべての投稿を表示
ラベル mysql の投稿を表示しています。 すべての投稿を表示

2020年5月29日金曜日

テーブルの使用容量確認

時々テーブルのデータが蓄積しすぎでディスク容量を圧迫されることがあります。その時以下のコマンドを使えばテーブル毎の容量、行数など情報をみることができます。

SELECT
  table_name,
  engine,
  table_rows,
  (data_length+index_length)/1024/1024/1024 as allGB,
  (data_length)/1024/1024/1024 as dataGB,
  (index_length)/1024/1024/1024 as indexGB
FROM
  information_schema.tables
WHERE
  table_schema=database()
ORDER BY
  (data_length+index_length) DESC;
+--------------------+--------+------------+-----------------+-----------------+----------------+
| TABLE_NAME         | ENGINE | TABLE_ROWS | allGB           | dataGB          | indexGB        |
+--------------------+--------+------------+-----------------+-----------------+----------------+
user           | InnoDB |   33543611 | 17.791976928711 | 16.137695312500 | 1.654281616211 |
| product       | InnoDB |      43979 |  0.893157958984 |  0.893157958984 | 0.000000000000 |
| comment | InnoDB |          0 |  0.000244140625 |  0.000015258789 | 0.000228881836 |
| category | InnoDB |          0 |  0.000244140625 |  0.000015258789 | 0.000228881836 |
+--------------------+--------+------------+-----------------+-----------------+----------------+

先程テーブル単位のデータ容量を確認できましたが、以下のクエリーを使えばデータベース単位での容量も確認できます。

SELECT
    table_schema, sum(data_length) /1024/1024/1024 AS GB
FROM
    information_schema.tables 
GROUP BY
    table_schema
ORDER BY      
    sum(data_length+index_length) DESC;
+---------------------+-----------------+
| table_schema        | GB              |
+---------------------+-----------------+
| bbs                 | 81.270685018040 |
user             | 13.613167230971 |
| product             |  1.102905860171 |
| test                |  0.629666061141 |
| log          |  0.017431590706 |
| auth                |  0.003235515207 |
| information_schema  |  0.000000000000 |
+---------------------+-----------------+


2020年1月17日金曜日

mysqlのload data 時に「ERROR 3948 (42000): Loading local data is disabled; this must be enabled on both the client and server sides」エラーが発生しました。

久々にlocalのDBにデータをloadしようと思ったら、以下のエラーがおきました。
mysql> load data local infile '/tmp/test.log' into table log FIELDS TERMINATED BY ',';
ERROR 3948 (42000): Loading local data is disabled; this must be enabled on both the client and server sides

以下の2つ手順でエラーを解消することができました。
Step1、 my.cnfファイルに 行「loose-local-infile = 1」を追加
 ※ my.cnf変更後にmysqlの再起動が必要 自分の場合コマンド「mysql.server restart」を実行しました。

Step2、mysqlにログイン時に「--local-infile=1」を付ける
 例: mysql -uroot --local-infile=1 logdb;

2019年8月21日水曜日

mysqlテーブルのプライマリーキーを削除

まず、テーブルの構造を表示

show create table テーブル名;

次に、テーブルの主キーを削除
alter table テーブル名 drop PRIMARY KEY;
最後、削除されたことを確認のため、再びテーブル構造の表示:
show create table テーブル名;

mysql文字化けのエラーがおきました。Warning (Code 1300): Invalid ujis character string: '\xE3\x83\xA1\xE3\x83\xBC...'

久々にmysqlに接続して、テーブルを作成しようとしたら、以下のエラーがおきました。
Warning (Code 1300): Invalid ujis character string: '\xE3\x83\xA1\xE3\x83\xBC...'
Warning (Code 1300): Invalid ujis character string: '\xE6\x9B\xB4\xE6\x96\xB0...'
Warning (Code 1300): Invalid ujis character string: '\xE4\xBD\x9C\xE6\x88\x90...'
Warning (Code 1300): Invalid ujis character string: '\xE6\x9B\xB4\xE6\x96\xB0...'
更に、select文で検索してみたら、文字化けしてしまいました。 一番手っ取り早い方法はset names utf8;コマンドをうつことです。
早速実行してみたら、文字化けは解消できました。
>set names utf8;
設定ファイルなど変更も色々方法がありますが、暫定対応としてここまでにしておきます。

2019年8月7日水曜日

mysqlのバージョンを確認する方法

まずmysqlに接続し、以下のsqlコマンドを使えばバージョン情報を確認することができます。
>select version();
出力は以下のようになります。

+-------------------------------------------+
| version()                                 |
+-------------------------------------------+
| 5.6.44-enterprise-commercial-advanced-log |
+-------------------------------------------+

2019年4月25日木曜日

macでmysqlを止められない時の対策

dockerを立ち上がろうとしたら、以下のエラーがでました。
ERROR: for test_db_1  Cannot start service db: driver failed programming external connectivity on endpoint test_db_1 (c0725b1665ce0d657ae65b915142e9e7dfbc8a3405d2b1599f9c26377a894526): Error starting userland proxy: Bind for 0.0.0.0:3306 failed: port is already allocated

ERROR: for db  Cannot start service db: driver failed programming external connectivity on endpoint test_db_1 (c0725b1665ce0d657ae65b915142e9e7dfbc8a3405d2b1599f9c26377a894526): Error starting userland proxy: Bind for 0.0.0.0:3306 failed: port is already allocated

ERROR: Encountered errors while bringing up the project.
つまり、3306ポートが既に使われています。

どんなサービスをそのポート番号を占めているのかを確認します。
 sudo lsof -i:3306
Password:
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME

mysqld  10115 _mysql   14u  IPv6 0xebcee56ac2cbc277      0t0  TCP *:mysql (LISTEN)
見事に自分のローカルのmysqlでした。
そして、「mysql.server stop」で止めようとしたら、エラーになりました。
 mysql.server stop
 ERROR! MySQL server PID file could not be found!
強制的にプロセスをkillしたら、また立ち上げれました。
そして、以下の方法もためしたました。全部だめでした。
$/usr/local/bin/mysql.server stop

 sudo /usr/local/mysql/bin/mysqld stop
sudo /usr/local/mysql/support-files/mysql.server stop
最後、以下の方法でようやく止めることはできました。
 sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

2017年8月28日月曜日

ERROR 1148 (42000) at line 1: The used command is not allowed with this MySQL version

mysqlにcvsファイルをロードしようとしたら、以下のエラーがおきました。

 ERROR 1148 (42000) at line 1: The used command is not allowed with this MySQL version

色々調査したところ、「--local-infile」を無効に設定されている可能性があります。

「--local-infile=1」をつけったら、無事にロードできました。完全なsql文は以下の感じになります。

mysql --local-infile=1  -u root -p -h 192.168.11.10 dbname -e "load data local infile './user.csv' ignore into table user ignore 1 lines;"

2017年2月26日日曜日

mysql テーブルロード時に、「The used command is not allowed with this MySQL version」エラーの問題

前日、新しいデータベースにデータをロードしようと思ったら、以下のエラーになりました。
mysql -u root -p -h 192.168.1.1 user -e"LOAD DATA LOCAL INFILE '/tmp/use.tsv' INTO TABLE user;"
Enter password:
ERROR 1148 (42000) at line 1: The used command is not allowed with this MySQL version


解決方法としては、「--local-infile=1」をつけて再実行することです。
mysql --local-infile=1 -u root -p -h 192.168.1.1 user -e"LOAD DATA LOCAL INFILE '/tmp/use.tsv' INTO TABLE user;"
Enter password:
「--local-infile=1」オプションを指定することによって、LOAD DATA LOCALを有効にします。逆に、「--local-infile=0」オプションを指定したらLOAD DATA LOCALを無効にします。


2017年1月18日水曜日

mysql 5.6でgrantをしたら、ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tm p/mysql_q4m.sock' のエラー

解決方法は、以下のコマンドを実行
# mysql_upgrade -u root -p

Looking for 'mysql' as: /usr/local/q4m/bin/mysql
Looking for 'mysqlcheck' as: /usr/local/q4m/bin/mysqlcheck
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.host                                         OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.servers                                      OK
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
Running 'mysql_fix_privilege_tables'...
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
freegame_queue.application_message_merge           OK
freegame_queue.game_playing_date                   OK
freegame_queue.guide_send_twitter_dm               OK
freegame_queue.mailbatch_mail                      OK
freegame_queue.messageapi_application_mail         OK
freegame_queue.messageapi_user_mail                OK
OK
Could not create the upgrade info file '/usr/local/q4m/var/mysql_upgrade_info' in the MySQL Servers datadir, errno: 13
[vagrant@010node mysql5.6q4m]$ sudo /usr/local/q4m/bin/mysql_upgrade -u root -p
Enter password:
Looking for 'mysql' as: /usr/local/q4m/bin/mysql
Looking for 'mysqlcheck' as: /usr/local/q4m/bin/mysqlcheck
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.host                                         OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.servers                                      OK
mysql.slave_master_info                            OK
mysql.slave_relay_log_info                         OK
mysql.slave_worker_info                            OK
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
Running 'mysql_fix_privilege_tables'...
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql_q4m.sock'
Warning: Using a password on the command line interface can be insecure.
freegame_queue.application_message_merge           OK
freegame_queue.game_playing_date                   OK
freegame_queue.guide_send_twitter_dm               OK
freegame_queue.mailbatch_mail                      OK
freegame_queue.messageapi_application_mail         OK
freegame_queue.messageapi_user_mail                OK
OK
[vagrant@010node mysql5.6q4m]$ /usr/local/q4m/bin/mysql -h localhost -P 13306 -u"root" -p"vagrant"
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.6.32-q4m-log Q4M

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

2015年10月17日土曜日

MAC OS環境でmysql サーバ起動するコマンド

久しぶりmysqlを使おうと思ったら、mysqlの起動コマンドを忘れてました。

メモとして、mysqlサーバ起動するコマンドは:
kaoru-no-MacBook-Pro:~ kaoru$ mysql.server start
Starting MySQL

. SUCCESS! 

2015年4月13日月曜日

mysql delete cascade制限を追加

mysql関連テーブル削除設定

例:memberテーブルとmember_attribute
  memberテーブルのレコードを削除する時、member_attributeにの関連レコードも削除する設定は、

alter table member_attribute add constraint fk_test1 foreign key (member_id) references members(member_id) on delete cascade;