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 | create database name ; 创建数据库 use databasename; 选择数据库 drop database name ; 直接删除数据库,不提醒 show tables; 显示表 describe tablename; 表的详细描述 select 中加上 distinct 去除重复字段 显示当前mysql版本和当前日期 select version(), current_date ; 修改mysql中root的密码: shell>mysql -u root -p mysql> update user set password = password (“root123″) where user =’root’; mysql> flush privileges 刷新权限 mysql>use dbname; 打开数据库 mysql>show databases; 显示所有数据库 mysql>show tables; 显示数据库mysql中所有的表 mysql> desc user ; 显示表mysql数据库中 user 表的列信息) grant 创建一个可以从任何地方连接到服务器的一个超管账户,必须分配一个密码 mysql> grant all privileges on *.* to 'user_name' @ 'localhost' identified by 'password' ; 格式: grant select on 数据库.* to 用户名@登录主机 identified by “密码” 删除授权: mysql> revoke all privileges on *.* from root@”%”; mysql> delete from user where user =”root” and host=”%”; mysql> flush privileges ; 重命名表: mysql > alter table t1 rename t2; 备份: mysqldump -hhostname -uusername -ppassword databasename > backup.sql; 恢复: mysql -hhostname -uusername -ppassword databasename< backup.sql; |
1 2 | /usr/ local /mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql |
1 2 | /usr/ local /mysql/bin/mysqld --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/ --initialize |
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 | [client] port = 3306 socket = /tmp/mysql.sock # default - character - set =utf8 [mysql] # default - character - set =utf8 [mysqld] port = 3306 socket = /tmp/mysql.sock basedir = /usr/ local /mysql datadir = /data/mysql open_files_limit = 3072 back_log = 103 max_connections = 512 max_connect_errors = 100000 table_open_cache = 512 external-locking = FALSE max_allowed_packet = 128M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 51 query_cache_size = 32M tmp_table_size = 96M max_heap_table_size = 96M slow_query_log = 1 slow_query_log_file = /data/mysql/slow.log log-error = /data/mysql/error.log long_query_time = 0.05 server-id = 1323306 log-bin = /data/mysql/mysql-bin sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 128M max_binlog_size = 1024M expire_logs_days = 7 key_buffer_size = 32M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M character - set -server=utf8 default -storage-engine=InnoDB binlog_format=row #gtid_mode= on #log_slave_updates=1 #enforce_gtid_consistency=1 interactive_timeout=100 wait_timeout=100 transaction_isolation = REPEATABLE - READ innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 1434M innodb_data_file_path = ibdata1:1024M:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 256M innodb_log_files_in_group = 2 innodb_max_dirty_pages_pct = 50 innodb_file_per_table = 1 innodb_locks_unsafe_for_binlog = 0 [mysqldump] quick max_allowed_packet = 32M |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | [client] port = 3306 socket = /data/mysql/mysql.sock [mysql] prompt= "\u@db \R:\m:\s [\d]> " no -auto-rehash [mysqld] user = mysql port = 3306 basedir = /usr/ local /mysql datadir = /data/mysql/ socket = /data/mysql/mysql.sock character - set -server = utf8mb4 skip_name_resolve = 1 open_files_limit = 65535 back_log = 1024 max_connections = 500 max_connect_errors = 1000000 table_open_cache = 1024 table_definition_cache = 1024 table_open_cache_instances = 64 thread_stack = 512K external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 768 query_cache_size = 0 query_cache_type = 0 interactive_timeout = 600 wait_timeout = 600 tmp_table_size = 32M max_heap_table_size = 32M slow_query_log = 1 slow_query_log_file = /data/mysql/slow.log log-error = /data/mysql/error.log long_query_time = 0.1 server-id = 3306101 log-bin = /data/mysql/mysql-binlog sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 1G max_binlog_size = 1G expire_logs_days = 7 gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates binlog_format = row relay_log_recovery = 1 relay-log-purge = 1 key_buffer_size = 32M read_buffer_size = 8M read_rnd_buffer_size = 4M bulk_insert_buffer_size = 64M lock_wait_timeout = 3600 explicit_defaults_for_timestamp = 1 innodb_thread_concurrency = 0 innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 transaction_isolation = REPEATABLE - READ innodb_buffer_pool_size = 1024M innodb_buffer_pool_instances = 8 innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 innodb_data_file_path = ibdata1:1G:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 2 innodb_max_undo_log_size = 4G innodb_io_capacity = 4000 innodb_io_capacity_max = 8000 innodb_flush_neighbors = 0 innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_purge_threads = 4 innodb_page_cleaners = 4 innodb_open_files = 65535 innodb_max_dirty_pages_pct = 50 innodb_flush_method = O_DIRECT innodb_lru_scan_depth = 4000 innodb_checksum_algorithm = crc32 innodb_lock_wait_timeout = 10 innodb_rollback_on_timeout = 1 innodb_print_all_deadlocks = 1 innodb_file_per_table = 1 innodb_online_alter_log_max_size = 4G internal_tmp_disk_storage_engine = InnoDB innodb_stats_on_metadata = 0 innodb_status_file = 1 innodb_status_output = 0 innodb_status_output_locks = 0 performance_schema = 1 performance_schema_instrument = '%=on' [mysqldump] quick max_allowed_packet = 32M |
1 2 3 4 5 6 7 8 9 10 | mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> explain select * from sbtest; + ----+-------------+--------+------+---------------+------+---------+------+-------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+-------------+--------+------+---------------+------+---------+------+-------+-------+ | 1 | SIMPLE | sbtest | ALL | NULL | NULL | NULL | NULL | 98712 | NULL | + ----+-------------+--------+------+---------------+------+---------+------+-------+-------+ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@node3 ~]# mysql -uroot -proot123 mysql> show variables like '%char%' ; + --------------------------+----------------------------------+ | Variable_name | Value | + --------------------------+----------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/ local /mysql/share/charsets/ | + --------------------------+----------------------------------+ 8 rows in set (0.00 sec) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | --single-transaction 用于保证innodb备份数据一致性,配合RR隔离级别使用;当发起事务,读取一个快照版本,直到备份结束时,都不会读取到本事务开始之后提交的数据;(很重要) -q, --quick 加 SQL_NO_CACHE 标示符来确保不会读取缓存里的数据-l --lock-tables 发起 READ LOCAL LOCK锁,该锁不会阻止读,也不会阻止新的数据插入 --master-data 两个值 1和2,如果值等于1,就会添加一个CHANGE MASTER语句(后期配置搭建主从架构) 如果值等于2,就会在CHANGE MASTER语句前添加注释(后期配置搭建主从架构) -c, --complete-insert; 导出完整sql语句 -d, --no-data; 不导出数据,只导表结构 -t, --no-create-info; 只导数据,不导表结构 -w, --where=name ; 按条件导出想要的数据 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 备份单个数据库或单个数据库中的指定表: mysqldump [OPTIONS] database [tb1] [tb2]… 备份多个数据库: mysqldump [OPTIONS] –databases [OPTIONS] DB1 [DB2 DB3...] 备份所有数据库: mysqldump [OPTIONS] – all -databases [OPTIONS] 利用mysql命令恢复数据: mysql -uroot -proot23 db_name < table_name.sql xtrabackup备份原理分析: 对于Innodb,它是基于Innodb的crash recovery功能进行备份。 |
1 2 3 4 5 | 首先需要创建备份目录:/opt/data/ innobackupex --no-timestamp --defaults-file=/etc/my.cnf --user root --socket=/tmp/mysql.sock --password root123 /opt/data/all-20170719-bak 注 --no-timestamp 该参数的含义:不需要系统创建时间目录,自己可以命名; |
1 2 3 4 5 6 | [root@node3 all -20170720-incr]# cat xtrabackup_checkpoints backup_type = incremental from_lsn = 267719862 to_lsn = 267720940 last_lsn = 267720940 compact = 0 |
1 2 3 4 5 6 | [root@node3 all -20170721-incr2]# cat xtrabackup_checkpoints backup_type = incremental from_lsn = 267720940 to_lsn = 267721260 last_lsn = 267721260 compact = 0 |
1 2 3 | ./innobackupex --no-timestamp --user root --socket=/tmp/mysql.sock --password root123 --defaults-file=/etc/my.cnf --incremental --incremental-basedir=/opt/data/all-20170719-bak /data/xtrabackup/all-20170720-incr |
1 2 3 | ./innobackupex --no-timestamp --user root --socket=/tmp/mysql.sock --password root123 --defaults-file=/etc/my.cnf --incremental --incremental-basedir=/data/xtrabackup/all-20170720-incr /data/xtrabackup/all-20170721-incr2 |
1 2 3 4 5 6 7 8 | innobackupex --user root --socket=/tmp/mysql.sock --password root123 --defaults-file=/etc/my.cnf --apply-log --redo-only +全备 innobackupex --user root --socket=/tmp/mysql.sock --password root123 --defaults-file=/etc/my.cnf --apply-log --redo-only 全备 --incremental-dir=增备1 innobackupex --user root --socket=/tmp/mysql.sock --password root123 --defaults-file=/etc/my.cnf --apply-log --redo-only 全备 --incremental-dir=增备2 innobackupex --user root --socket=/tmp/mysql.sock --password root123 --defaults-file=/etc/my.cnf --apply-log +全备 |
本文出自 “张甦的博客” 博客