Mysql useful commands

Here will be some commands and their examples with explanations.

  • create DB:
    CREATE DATABASE `my_db` CHARACTER SET utf8 COLLATE utf8_general_ci;
  • show db collection and charset:
    USE db_name;
    SELECT @@character_set_database, @@collation_database;
  • create user:
    CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
  • grant permissions to some db:
    GRANT ALL PRIVILEGES ON 'DB_name'.* TO 'monty'@'localhost';
  • show processlist (who’s connected and if they’re currently running anything):
    SELECT * FROM information_schema.processlist \G
    show processlist;
  • enable queue loggining:
    SET global log_output='table';
    SET global general_log=1;
  • enable slow logs:
    SET global long_query_time=2;
    SET global slow_query_log=1;
  • mysql skipp counter and restart replication;
    STOP slave;
    SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
    START slave;
    show slave status \G
  • enable mysql profiling:
    set profiling=1;
    query;
    show profiles;
    show profile for query qry_number;
    set profiling=0;
  • skip specific error in replication. Add below to my.cnf:
    slave_skip_errors error_number
  • log-slave-updates
    innodb_flush_log_at_trx_commit=2 (default = 1)
    innodb_flush_method=O_DIRECT
  • create mysql replication:
    GRANT replication slave ON *.* TO "repl_user"@"%" IDENTIFIED BY "password";
    CHANGE MASTER TO MASTER_HOST='master_host_fqdn', master_user='repl_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='binlog_file_name', MASTER_LOG_POS=pos_number;
  • Enable read-only mode:
    SET GLOBAL read_only = ON;
    or add to /etc/mysql/my.cnf: read_only=1
  • change column type in table:
    ALTER TABLE table_name MODIFY column_name type;
  • add column to table:
    ALTER TABLE table_name ADD column_name type;
  • Change user password:
    ALTER USER some_user@'%' IDENTIFIED BY 'new_password';
  • compare databases between servers (also available for the same server):
    mysqldbcompare --server1=root@localhost:3306 --server2=root:pwd@mysql_url:3306 --changes-for=server2
    –changes-for – means show changes for server. I.e. above should be compared with server1
No comment yet
Leave a Reply

Your email address will not be published. Required fields are marked *