首页 > 代码库 > MySQL管理_数据库常用命令
MySQL管理_数据库常用命令
1.数据库赋权
进入mysql数据库:mysql> use mysql;给root用户设置新密码:mysql> update user set password=password("新密码") where user="root";刷新数据库mysql> flush privileges;nagios监控mysql> GRANT PROCESS, REPLICATION CLIENT ON *.* TO ‘nagiosshow‘@‘10.172.172.12‘ IDENTIFIED BY PASSWORD ‘*79B36E3D5F430AF5B15934D61D71C031B6502834‘;查看用户信息use mysql;select User,Host,Password from user;查看用户权限show grants for root@‘localhost‘;删除用户delete user form user where Host=‘xxxx‘ and User=‘xxxx‘;
2.新建数据库
查看已有用户:mysql> use mysql;mysql> select Host,User,Password from user;新建数据库CREATE DATABASE marketing_base DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;新建用户并赋权grant all privileges on marketing_base.* to dysql@‘%‘ identified by ‘1Qaz2wsx‘;mysql -uroot -p123456 --default-character-set=gb2312 test<F:/pushingdb.sql mysql -uroot -p1qaz2wsx --default-character-set=utf8 ryp_production < cnapsbank.sql
3.导出数据库
导出数据时排除某些表的数据
可以运行两条命令,可以一起运行。首先导出表结构:mysqldump -uxxx -p -d db_name > db_name.sql然后导出你要的数据:mysqldump -uxxx -p dbname --ignore-table=test.t --ignore-table=test.t1 ...>>test.sql;仅导出数据库的数据:mysqldump -uxxx -p -t db_name > db_data.sql注:--ignore-table=xx “=” 左右不能有空格。导出数据库包含pos、file值mysqldump -uroot --quick --flush-logs --master-data=http://www.mamicode.com/1 -p ryp1_production > ryp1_production20140924.sql
4.导出表结构
命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据 mysqldump -h localhost -uroot -p123456 database > dump.sql 导出单个数据表结构和数据 mysqldump -h localhost -uroot -p123456 database table > dump.sql 导出整个数据库结构(不包含数据) /usr/local/mysql/bin/mysqldump -uroot -d entrym> dump.sql 导出单个数据表结构(不包含数据) mysqldump -h localhost -uroot -p123456 -d database table > dump.sql字段分隔符mysqldump -uroot -p hsb MERCHANT_INFO --tab="/tmp/" --fields-terminated-by="#;@"mysql> select * into outfile ‘/tmp/CASH_USE_INFO.sql‘ FIELDS TERMINATED BY ‘#;@‘ from CASH_USE_INFO;mysql> select * into outfile ‘/tmp/MERCHANT_ORDER.sql‘ FIELDS TERMINATED BY ‘#;@‘ from MERCHANT_ORDER where MERCHANT_ID in(select ID from MERCHANT_INFO); 导出存储过程 mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名
5.主从同步设置
主数据库GRANT REPLICATION SLAVE ON *.* TO ‘backup‘@‘192.168.252.%‘ IDENTIFIED BY ‘123456‘;mysql> show master status\G从数据库mysql> change master to master_host=‘192.168.252.150‘, master_user=‘backup‘, master_password=‘123456‘, master_log_file=‘mysql-bin.000003‘, master_log_pos=107;mysql> start slave;mysql> SHOW SLAVE STATUS\G
6.查看数据库大小
进入information_schema数据库(存放了其他的数据库的信息)use information_schema;查询所有数据的大小:select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables;查看指定数据库home的大小select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables where table_schema=‘home‘;查看指定数据库home中 members 表的大小select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables where table_schema=‘home‘ and table_name=‘members‘;
7.查询新建表索引
查看表是否存在索引(二选一)show index from table_name from db_nameshow index from db_name.table_name查看表结构desc table_name为表字段(field_name)创建索引create index field_name_index on table_name(field_name);
8.外部执行sql语句
mysql -uroot -p -e “seclect * from ” > seclect.sql
9.KILL Mysql Processlist ID
#!/bin/bashDATAFILE=`date +%Y%m%d%s`/usr/bin/mysql -uroot -p123456 -e "select concat(‘KILL ‘,id,‘;‘) from information_schema.processlist where user=‘root‘ into outfile ‘/tmp/$DATAFILE.txt‘;"KILLCOM=`/usr/bin/mysql -uroot -p123456 -e "source /tmp/$DATAFILE.txt"` git fetch 1038 git merge origin/develop
10锁表处理
use information_schemaselect * from INNODB_LOCK_WAITSselect r.trx_id as waiting_trx_id,r.trx_mysql_thread_id as waiting_thread,timestampdiff(second,r.trx_wait_started,current_timestamp) as wait_time,r.trx_query as waiting_query,l.lock_table as waiting_table_lock,b.trx_id as blocking_trx_id,b.trx_mysql_thread_id as blocking_thread,substring(p.host,1,instr(p.host,‘:‘)-1) as blocking_host,substring(p.host,instr(p.host,‘:‘)+1) as blocking_port,if(p.command=‘sleep‘,p.time,0) as idle_in_trx,b.trx_query as blocking_queryfrom information_schema.INNODB_LOCK_WAITS as w inner join information_schema.innodb_trx as b on b.trx_id=w.blocking_trx_id inner join information_schema.innodb_trx as r on r.trx_id=w.requesting_trx_idinner join information_schema.INNODB_LOCKS as l on w.requested_lock_id =l.lock_id left join information_schema.PROCESSLIST as p on p.id=b.trx_mysql_thread_idorder by wait_time desc\G;
11.分析频繁操作的表
mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000009 | awk ‘/###/{if($0~/UPDATE|INSERT|DELETE/)count[$2" "$NF]++}END{for(i in count)print i,"\t",count[i]}‘ | column -t | sort -k3nr >>~/$$.sql & 2>/dev/null
12.数据库分析工具
yum install percona-toolkit-2.2.8-1.noarchpt-query-digest mysql-slow-queries_20150313.log
13.跳过某个同步错误
slave stop;set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;slave start;
14.开启慢查询日志
配置文件开启(需重启)vi /etc/my.cnflong_query_time = 2log-slow-queries=/var/log/slow.log在线开启(无需重启)set long_query_time=2set global slow_query_log = 1;set global slow_query_log_file = "/var/log/slow.log";
15.存储过程执行权限
definer (‘lpdba‘@‘%‘) 确定定义者为lpdbagrant all on mysql.* to lpdba@‘%‘ identified by ‘l1ghtp@l3‘; grant execute on test.* to lpdba@‘%‘ identified by ‘l1ghtp@l3‘;
16.开启事件
SHOW VARIABLES LIKE ‘event_scheduler‘;SET GLOBAL event_scheduler = ON;
17.修改数据库触发器
查看数据库(vas_manage)的触发器创建语句use vas_manage;show CREATE TRIGGER before_update_game_product;删除触发器drop TRIGGER before_update_game_product;创建触发器CREATE TRIGGER before_update_game_product BEFORE UPDATE ON game_productFOR EACH ROWBEGIN IF LOCATE(" ",new.game_type)>0 THEN set NEW.game_type=REPLACE(NEW.game_type," "," "); END if;END
MySQL管理_数据库常用命令
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。