zoukankan      html  css  js  c++  java
  • mysql的常用操作语句记录


    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;
    start slave;

    跳过某种事件的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;

  • 相关阅读:
    前端代码规范
    AD 对联
    leaflet 入门笔记
    在Mac OS X上安装Git
    在Mac上开启C的新征程
    Python编程基础
    GitHub的使用(Git Shell)
    《网页设计心理学》Susan M.Weinschenk 小结精粹
    【问题】做图片验证码时乱码了,在header前加上ob_clean()就能神奇的显示?!
    --allow-file-access-from-files 命令的使用
  • 原文地址:https://www.cnblogs.com/reblue520/p/14075297.html
Copyright © 2011-2022 走看看