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';
GRANT ALL PRIVILEGES ON *.* TO 'midlis'@'%' IDENTIFIED BY 'midlis24' WITH GRANT OPTION; USE mysql; CREATE USER 'midlis@%' IDENTIFIED BY 'midlis24'; CREATE USER 'midlis' IDENTIFIED BY 'midlis24'; select User,Host,Password from user;
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=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_name
show 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/bash DATAFILE=`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_schema select * from INNODB_LOCK_WAITS select 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_query from 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_id inner 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_id order 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.noarch
pt-query-digest mysql-slow-queries_20150313.log
13.跳过某个同步错误
slave stop; set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; slave start;
14.开启慢查询日志
配置文件开启(需重启) vi /etc/my.cnf long_query_time = 2 log-slow-queries=/var/log/slow.log 在线开启(无需重启) set long_query_time=2 set global slow_query_log = 1; set global slow_query_log_file = "/var/log/slow.log";
15.存储过程执行权限
definer ('lpdba'@'%') 确定定义者为lpdba grant 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_product FOR EACH ROW BEGIN IF LOCATE(" ",new.game_type)>0 THEN set NEW.game_type=REPLACE(NEW.game_type," "," "); END if; END