Access MySQL from external host: mysql -h ip u root -p

After logging in as root, check the registered users and register additional external IP addresses.

Connect to database, mysql -u root -p
-u: specify a user option
-u root: meaning like “by root user”
-p: login with password option

-Add an external IP address
Configure John user to access xxxdb from 192.168.1.*.

mysql> grant all privileges on xxxdb.* to john@”192.168.1.%” identified by ‘hoge’ with grant option;

How to login from an external host
$ mysql -h 192.168.1.12 -u John -p
port number: 3306 is running.
If it can not connect, check TCP Wrapper and IP tables.

How to login by specifying a database:
$ mysql -h 192.168.1.12 dbname -u John -p

To specify a port from an external host and login
$ mysql -h 192.168.1.12 –port=3307 -u John -p
When connecting to a MySQL server whose port number is set other than 3306, also specify the port number.

mysql log variation

-Error log
-General Query log
-Binary log
-Slow query log

Error log
The error log, which is also called system logging, log the startup, execution, stop logging of mysqld(server) error information of the problem that occurred, and so on.

cat /etc/my.cnf
log-error=/var/log/mysqld.log
cat /var/log/mysqld.log

General Query log
The general query log is also called query (SQL) logging, and mysqld (server) logs the connection with the client and the query (SQL) executed.

Binary log
Binary logs are also called binary (backup) logging and log statements of mysqld(server) data change as binary information. It is also used for replication.

-sample

log_bin="/var/log/mysql/bin.log"
log_bin_index="/var/log/mysql/bin.list"
max_binlog_size=1M
expire_logs_days=1

slow query log
Slow query logs are also referred to as slow query (debug) logging, logging of queries (SQL) that took longer than the number of seconds specified by long_query_time on mysqld (server) or queries that did not use index (SQL).

slow_query_log, general_log

mysql> show variables like ‘slow_query_log’;
+—————-+——-+
| Variable_name | Value |
+—————-+——-+
| slow_query_log | OFF |
+—————-+——-+
1 row in set (0.12 sec)

mysql> show variables like ‘general_log’;
+—————+——-+
| Variable_name | Value |
+—————+——-+
| general_log | OFF |
+—————+——-+
1 row in set (0.00 sec)

DELIMITER

With DELIMITER you can specify the end of SQL statement to another character, not “;”.

mysql> DELIMITER //
mysql> create definer = 'user'@'%' function 'msec'() returns char(26) charset sjis
    -> BEGIN
    -> return ( select replace(replace(Now(6), ' ','-'), ':', '.') from dual);
    -> END
    -> //
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 ''msec'() returns char(26) charset sjis
BEGIN
return ( select replace(replace(Now' at line 1

create table clog (
 CAMPAIGN_CODE char(5) NOT NULL,
 ENO char(40) NOT NULL,
 LINK_ITEM_NO char(3) NOT NULL,
 CLICKSTAMP char(26) NOT NULL,
 FREE1 varchar(253) DEFAULT NULL,
 FREE2 varchar(253) DEFAULT NULL,
 FREE3 varchar(253) DEFAULT NULL,
 FREE4 varchar(253) DEFAULT NULL,
 FREE5 varchar(253) DEFAULT NULL,
 FREE6 varchar(253) DEFAULT NULL,
 FREE7 varchar(253) DEFAULT NULL,
 FREE8 varchar(253) DEFAULT NULL,
 FREE9 varchar(253) DEFAULT NULL,
 FREE10 varchar(253) DEFAULT NULL,
 INSSTAMP char(26) NOT NULL,
 UPDSTAMP char(26) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=sjis;

create table cre (
 CAMPAIGN_CODE char(5) NOT NULL,
 LINO char(3) NOT NULL,
 START_DATE date NOT NULL,
 START_TIME char(4) NOT NULL,
 END_DATE date NOT NULL,
 END_TIME char(4) NOT NULL,
 MLURL varchar(512) NOT NULL,
 FREE1 varchar(253) DEFAULT NULL,
 FREE2 varchar(253) DEFAULT NULL,
 FREE3 varchar(253) DEFAULT NULL,
 FREE4 varchar(253) DEFAULT NULL,
 FREE5 varchar(253) DEFAULT NULL,
 FREE6 varchar(253) DEFAULT NULL,
 FREE7 varchar(253) DEFAULT NULL,
 FREE8 varchar(253) DEFAULT NULL,
 FREE9 varchar(253) DEFAULT NULL,
 FREE10 varchar(253) DEFAULT NULL,
 INSSTAMP char(26) NOT NULL,
 UPDSTAMP char(26) NOT NULL,
 PRIMARY KEY(CAMPAIGN_CODE, LINO),
 UNIQUE KEY SQL0123456(CAMPAIGN_CODE, LINO)
) ENGINE=InnoDB DEFAULT CHARSET=sjis;

DELIMITER
create definer = 'user'@'%' function 'msec'() returns char(26) charset sjis
BEGIN
  return ( select replace(replace(Now(6), ' ','-'), ':', '.') from dual);
END
//
DELIMITER ;


insert into cre (
 CAMPAIGN_CODE,
 LINO,
 START_DATE,
 START_TIME,
 END_DATE,
 ENT_TIME,
 MLURL,
 INSSTAMP,
 UPDSTAMP
) values (
 '00AF',
 '001',
 '2019-01-01',
 '0000',
 '2022-12-31',
 '2345',
 'http://localhost',
 '2019-01-01-00.00.00.000000',
 '2019-01-01-00.00.00.000000'
);

grant select, insert, execute on dev to ‘hoge@%’ indeintified by ‘password’

grant select, insert, update on ${table_name}.* to user@’%’ identified by ‘password’

mysql> create database dev;
Query OK, 1 row affected (0.14 sec)

mysql> use dev;
Database changed
mysql> grant select, insert, update, delete execute on dev.* to 'user'@'%' identified by 'password';
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 'execute on dev.* to 'user'@'%' identified by 'password'' at line 1
mysql> grant select, insert, update, delete, execute on dev.* to 'user'@'%' identified by 'password';
Query OK, 0 rows affected (0.19 sec)

ほーーーーーー

lower_case_table_names

MySQL has a variable named lower_case_table_names, which is a parameter that sets whether to distinguish case of table names.

0
Store database names and table names in uppercase and lowercase letters. The comparison of these names is also case sensitive. If the file system of the OS on which the MySQL server runs is case insensitive(eg OS X or windows), it should not be set to this value. If you set this value to 0 on such a file system and access the MyISAM table name by mixing case, sometimes the index may be destroyed.

1
Everything is rounded to lowercase when storing table names and database names. The comparison of names is case insensitive.

2
When storing the table name and database name, store it as it is (that is, store it-while preserving upper case letters and lower case letters). When looking up, these names are converted to lowercase letters, and comparison of names is done without distinguishing uppercase and lowercase letters. Note that this function work only on file systems that do not distinguish between uppercase and lowercase letters. The table name of InnoDB is stored in all lowercase letters as when setting lower_case_table_names = 1.

[vagrant@localhost ~]$ cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Settings user and group are ignored when systemd is used (fedora >= 15).
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
user=mysql

# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so

# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1

# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema

[mysql]
default-character-set=utf8

ないですね、ということはmy.cnfに追加できるってことかな。。

character_set_server

character_set_client: The sql statement issued on the client side becomes this character code
character_set_connection: converts characters received from the client into this character code
character_set_database: character code of the DB currently being referenced
character_set_results: The search result sent to the client is this character code
character_set_server: Default character code when creating DB
character_set_system: utf8 is always used in the character set used by the system

character-set-filesystem: file system character code

default-character-set

[vagrant@localhost ~]$ cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Settings user and group are ignored when systemd is used (fedora >= 15).
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
user=mysql

# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so

# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1

# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema

[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

ふむふむ、ありました。なるほどー
[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8

mysqlbinlog

mysqlbinlog is “information recorded in a form that can trace the operation history when changing the contents of the database, such as CREATE TABLE statement or INSERT statement”.

[vagrant@localhost ~]$ mysqlbinlog
mysqlbinlog Ver 3.4 for Linux at x86_64
Copyright (c) 2000, 2018, 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.

Dumps a MySQL binary log in a format usable for viewing or for piping to
the mysql command line client.

Usage: mysqlbinlog [options] log-files
  -?, --help          Display this help and exit.
  --base64-output=name
                      Determine when the output statements should be
                      base64-encoded BINLOG statements: 'never' disables it and
                      works only for binlogs without row-based events;
                      'decode-rows' decodes row events into commented
                      pseudo-SQL statements if the --verbose option is also
                      given; 'auto' prints base64 only when necessary (i.e.,
                      for row-based events and format description events).  If
                      no --base64-output[=name] option is given at all, the
                      default is 'auto'.
  --bind-address=name IP address to bind to.
  --character-sets-dir=name
                      Directory for character set files.
  -d, --database=name List entries for just this database (local log only).
  --debug-check       Check memory and open file usage at exit .
  --debug-info        Print some debug info at exit.
  --default-auth=name Default authentication client-side plugin to use.
  -D, --disable-log-bin
                      Disable binary log. This is useful, if you enabled
                      --to-last-log and are sending the output to the same
                      MySQL server. This way you could avoid an endless loop.
                      You would also like to use it when restoring after a
                      crash to avoid duplication of the statements you already
                      have. NOTE: you will need a SUPER privilege to use this
                      option.
  -F, --force-if-open Force if binlog was not closed properly.
                      (Defaults to on; use --skip-force-if-open to disable.)
  -f, --force-read    Force reading unknown binlog events.
  -H, --hexdump       Augment output with hexadecimal and ASCII event dump.
  -h, --host=name     Get the binlog from server.
  -l, --local-load=name
                      Prepare local temporary files for LOAD DATA INFILE in the
                      specified directory.
  -o, --offset=#      Skip the first N entries.
  -p, --password[=name]
                      Password to connect to remote server.
  --plugin-dir=name   Directory for client-side plugins.
  -P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
                      memory).
  -R, --read-from-remote-server
                      Read binary logs from a MySQL server. This is an alias
                      for read-from-remote-master=BINLOG-DUMP-NON-GTIDS.
  --read-from-remote-master=name
                      Read binary logs from a MySQL server through the
                      COM_BINLOG_DUMP or COM_BINLOG_DUMP_GTID commands by
                      setting the option to either BINLOG-DUMP-NON-GTIDS or
                      BINLOG-DUMP-GTIDS, respectively. If
                      --read-from-remote-master=BINLOG-DUMP-GTIDS is combined
                      with --exclude-gtids, transactions can be filtered out on
                      the master avoiding unnecessary network traffic.
  --raw               Requires -R. Output raw binlog data instead of SQL
                      statements, output is to log files.
  -r, --result-file=name
                      Direct output to a given file. With --raw this is a
                      prefix for the file names.
  --secure-auth       Refuse client connecting to server if it uses old
                      (pre-4.1.1) protocol.
                      (Defaults to on; use --skip-secure-auth to disable.)
  --server-id=#       Extract only binlog entries created by the server having
                      the given id.
  --server-id-bits=#  Set number of significant bits in server-id
  --set-charset=name  Add 'SET NAMES character_set' to the output.
  -s, --short-form    Just show regular queries: no extra info and no row-based
                      events. This is for testing only, and should not be used
                      in production systems. If you want to suppress
                      base64-output, consider using --base64-output=never
                      instead.
  -S, --socket=name   The socket file to use for connection.
  --start-datetime=name
                      Start reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  -j, --start-position=#
                      Start reading the binlog at position N. Applies to the
                      first binlog passed on the command line.
  --stop-datetime=name
                      Stop reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  --stop-never        Wait for more data from the server instead of stopping at
                      the end of the last log. Implicitly sets --to-last-log
                      but instead of stopping at the end of the last log it
                      continues to wait till the server disconnects.
  --stop-never-slave-server-id=#
                      The slave server_id used for --read-from-remote-server
                      --stop-never.
  --stop-position=#   Stop reading the binlog at position N. Applies to the
                      last binlog passed on the command line.
  -t, --to-last-log   Requires -R. Will not stop at the end of the requested
                      binlog but rather continue printing until the end of the
                      last binlog of the MySQL server. If you send the output
                      to the same MySQL server, that may lead to an endless
                      loop.
  -u, --user=name     Connect to the remote server as username.
  -v, --verbose       Reconstruct pseudo-SQL statements out of row events. -v
                      -v adds comments on column data types.
  -V, --version       Print version and exit.
  --open-files-limit=#
                      Used to reserve file descriptors for use by this program.
  -c, --verify-binlog-checksum
                      Verify checksum binlog events.
  --binlog-row-event-max-size=#
                      The maximum size of a row-based binary log event in
                      bytes. Rows will be grouped into events smaller than this
                      size if possible. This value must be a multiple of 256.
  --skip-gtids        Do not print Global Transaction Identifier information
                      (SET GTID_NEXT=... etc).
  --include-gtids=name
                      Print events whose Global Transaction Identifiers were
                      provided.
  --exclude-gtids=name
                      Print all events but those whose Global Transaction
                      Identifiers were provided.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
base64-output                     (No default value)
bind-address                      (No default value)
character-sets-dir                (No default value)
database                          (No default value)
debug-check                       FALSE
debug-info                        FALSE
default-auth                      (No default value)
disable-log-bin                   FALSE
force-if-open                     TRUE
force-read                        FALSE
hexdump                           FALSE
host                              (No default value)
local-load                        (No default value)
offset                            0
plugin-dir                        (No default value)
port                              0
read-from-remote-server           FALSE
read-from-remote-master           (No default value)
raw                               FALSE
result-file                       (No default value)
secure-auth                       TRUE
server-id                         0
server-id-bits                    32
set-charset                       (No default value)
short-form                        FALSE
socket                            (No default value)
start-datetime                    (No default value)
start-position                    4
stop-datetime                     (No default value)
stop-never                        FALSE
stop-never-slave-server-id        -1
stop-position                     18446744073709551615
to-last-log                       FALSE
user                              (No default value)
open-files-limit                  64
verify-binlog-checksum            FALSE
binlog-row-event-max-size         4294967040
skip-gtids                        FALSE
include-gtids                     (No default value)
exclude-gtids                     (No default value)

あれ? default-character-setがない!?