mysql的常用操作語句記錄
阿新 • • 發佈:2020-12-03
1.備份資料庫的操作
# 匯出單個數據庫
mysqldump -uroot -p --default-character-set='utf8' cms_management > cms_management20200218.sql
# 只匯出資料庫結構
mysqldump --opt -d cms_management -uroot -p --default-character-set='utf8' > cms_management20200218.sql
# 匯出所有資料庫
all-databases
2.設定主從常用的問題處理
# 主從跳過某條資料語句
stop slave;
set global sql_slave_skip_counter=1;
跳過某種事件的my.cnf配置:
[mysqld]下加一行 slave_skip_errors = 1062
MySQL最大可使用記憶體(M):
select (@@key_buffer_size +@@innodb_buffer_pool_size + @@tmp_table_size + @@max_connections*(@@read_buffer_size + @@read_rnd_buffer_size + @@sort_buffer_size + @@join_buffer_size + @@binlog_cache_size + @@thread_stack) )/1024/1024 as "Total_AllMem result";
3.資料庫監控中用到的預設配置 .my.cnf
[client]
user=username
password=password
4.binlog日誌的清理和設定
# 設定bin-log日誌儲存時長,如果時間過長會導致磁碟佔用空間很大
# 儲存15天,然後刪除超過15天的日誌
mysql> set global expire_logs_days=15;
Query OK, 0 rows affected (0.00 sec)
# 刪除超過15天的日誌
mysql> flush logs;
Query OK, 0 rows affected (0.18 sec)
# 清理 某個binlog之前的binlog檔案
purge binary logs to 'mysql-bin.000356';
將指定時間之前的binlog清掉:
purge binary logs before '2019-05-29 00:00:00';
5.mysql資料庫的授權
mysql5.7 授權:
GRANT RELOAD, PROCESS, SUPER, LOCK TABLES, REPLICATION CLIENT, CREATE TABLESPACE ON *.* TO 'xtrabackup'@'localhost' identified by 'pass';
flush privileges;
grant select,insert,update,delete on ws_dr_member.* to ws_dr_member_user@"%" identified by "pass";
# 賦權給一個賬號多個數據庫許可權
grant select,insert,update,delete on spyapolloconfigdb.* to spyapoll_user@"%" identified by "pass";
grant select,insert,update,delete on spyapolloportaldb.* to spyapoll_user@"%";
# 授權某個資料庫儲存過程的建立,修改,執行的許可權
GRANT CREATE ROUTINE,ALTER ROUTINE, EXECUTE ON cms_marketing.* TO 'cms_marketing'@'%';
GRANT CREATE TEMPORARY TABLES, EXECUTE, CREATE VIEW, CREATE ROUTINE, ALTER ROUTINE ON `cms_marketing`.* TO 'cms_marketing'@'%'
# 新增root localhost的許可權
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'pass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'pass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
# mysql5.7修改root賬號密碼
update user set authentication_string = password('pass'), password_expired = 'N', password_last_changed = now() where user = 'root' host='localhost';
# 修改root密碼
mysql> update mysql.user set authentication_string = password('pass'), password_expired = 'N', password_last_changed = now() where user = 'root';
mysql> flush privileges;
# 檢視授權,及收回授權
mysql> show grants for antiadmin@'localhost';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for antiadmin@localhost |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, ALTER, SUPER, LOCK TABLES, REPLICATION CLIENT, CREATE TABLESPACE ON *.* TO 'antiadmin'@'localhost' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
mysql> revoke CREATE, DROP, RELOAD, PROCESS, ALTER, SUPER, LOCK TABLES, REPLICATION CLIENT, CREATE TABLESPACE ON *.* from 'antiadmin'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for antiadmin@'localhost';
+------------------------------------------------------------------------+
| Grants for antiadmin@localhost |
+------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'antiadmin'@'localhost' |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql8.0授權:
# 備份賬號
create user 'xtrabackup'@'localhost' identified with mysql_native_password by 'pass';
GRANT RELOAD, PROCESS, SUPER, LOCK TABLES, REPLICATION CLIENT, CREATE TABLESPACE ON *.* TO 'xtrabackup'@'localhost';
# 新增監控使用者
create user 'zabbix_montior_user'@'localhost' identified with mysql_native_password by 'pass';
grant select,process,replication client on *.* to zabbix_montior_user@'localhost';
flush privileges;
mysql8.0建立賬號,新增許可權:
CREATE USER 'online_video_user'@'%' IDENTIFIED BY 'pass';
grant all privileges on vidcloud_res_oa.* TO 'online_video_user'@'%' WITH GRANT OPTION;
grant all privileges on ove.* TO 'online_video_user'@'%' WITH GRANT OPTION;
CREATE USER 'online_video_back_user'@'%' IDENTIFIED BY 'pass';
grant all privileges on vidcloud_res_oa.* TO 'online_video_back_user'@'%';
grant all privileges on ove.* TO 'online_video_back_user'@'%';
flush privileges;
# 修改root密碼
alter user'root'@'localhost' IDENTIFIED BY 'pass';
# 新增root賬號
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'pass';
grant all privileges on *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
# 回收許可權
語法 : revoke 許可權 on 資料庫.資料庫表 from '使用者名稱'@'ip';
案例:revoke all privileges on ove.* from 'graphics_user_write'@'172.30.0.%';
CREATE USER 'ove_user'@'172.30.0.%' IDENTIFIED BY 'pass';
grant select,insert,update,delete ON ove.* TO 'ove_user'@'172.30.0.%';
CREATE USER 'ove_read'@'172.30.0.%' IDENTIFIED BY 'pass';
grant select on ove.* TO 'ove_read'@'172.30.0.%';
CREATE USER 'vid_user'@'172.30.0.%' IDENTIFIED BY 'pass';
grant select,insert,update,delete on vidcloud_res_oa.* TO 'vid_user'@'172.30.0.%';
# 批量kill mysql的程序
for id in `mysqladmin -uroot -p"pass" processlist|grep -i "unauthenticated"|awk '{print $2}'`
do
mysqladmin -uroot -p"pass" kill ${id}
done
## 主從複製錯誤
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 0 failed executing transaction 'ANONYMOUS' at master log mysql-bin.000308, end_log_pos 27654. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
Replicate_Ignore_Server_Ids:
# 主庫檢視binlog日誌的內容
[root@newcms:/data/mysql_data]# mysqlbinlog --base64-output=DECODE-ROWS -v --start-position=27654 --stop-position=27654 mysql-bin.000308|more
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
# mysql8 刪除使用者
DROP USER 'xtrabackup'@'localhost';
# 建立備份賬號
create user xtrabackup@'localhost' identified by "pass";
grant selecton *.* to xtrabackup@'localhost';
grant file on *.* to xtrabackup@'localhost';
grant show view on *.* to xtrabackup@'localhost';
grant lock tables on *.* to xtrabackup@'localhost';
grant trigger on *.* to xtrabackup@'localhost';
grant EVENT on *.* to xtrabackup@'localhost';
grant reload on *.* to xtrabackup@'localhost';
GRANT BACKUP_ADMIN ON *.* TO xtrabackup@'localhost';
grant process on *.* to xtrabackup@'localhost';
grant super on *.* to xtrabackup@'localhost';
grant Replication client on *.* to xtrabackup@'localhost';
GRANT SELECT ON performance_schema.variables_info TO 'xtrabackup'@'localhost'; # For release 8.0.16 and later
GRANT SELECT ON performance_schema.* TO 'xtrabackup'@'localhost'; # For release 8.0.16 and later
GRANT CREATE, INSERT, DROP, UPDATE ON mysql.backup_progress TO 'xtrabackup'@'localhost';
GRANT CREATE, INSERT, DROP, UPDATE, SELECT, ALTER ON mysql.backup_history TO 'xtrabackup'@'localhost';
GRANT SELECT ON performance_schema.replication_group_members TO 'xtrabackup'@'localhost';
ALTER USER xtrabackup@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass';
ALTER USER `xtrabackup`@`localhost` PASSWORD EXPIRE NEVER;
ALTER USER `xtrabackup`@`localhost` WITH MAX_USER_CONNECTIONS 20;
flush privileges;