mysql初级运维使用技巧

    整理了一下,工作中用到的最高的关于mysql的一些命令和使用技巧,分享给刚接触mysql的小伙伴么。

    最近写项目用到mysql一些基础命令,时间长了就忘了,特置顶随时查找。

1      mysql最基础

1.1     mysql安装

   建议新人安装mysql直接使用yum安装即可,大牛们已经对其优化的差不多了,真正玩牛了再搞源码安装:

   yum -y install mysql mysql-server mysql-devel

   注意,如果是centos库和有些国内yum源的mysql版本都比较低,如果想要安装高版本mysql,需要换源,具体操作如下:

         rpm -ivhhttp://rpms.famillecollet.com/enterprise/remi-release-6.rpm
         rpm--import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
         yum--enablerepo=remi update mysql mysql-server mysql-devel

   一定要注意,将mysql服务设置为开机自启动:

chkconfigmysqld on

1.2     密码设置与用户授权

1.2.1     root密码设置

1.   shell命令行:mysqladmin -uroot -ppassword 'new_password'
2.   mysql命令行:upload mysql.user set password=password("new_passwd")where user="root" and host="localhost";

1.3     账号授权

1.   账号授权:grant all privileges on *.* to user_name@'%' identified by '1234';
2.   权限回收:revoke  GRANT ALL PRIVILEGESON *.* from 'user_name'@'%';

这个只属于新手用的命令,更详细的会在下文中详细介绍

2      mysql使用技巧

2.1     中文乱码

1.   首先进入mysql命令行看下mysql使用什么编码方式:

                   showvariables like "%char%";

2.   在命令行零时修改mysql编码方式:

                   SETcharacter_set_client='utf8';
                   SETcharacter_set_connection='utf8';
                   SETcharacter_set_results='utf8';

3.   在/etc/my.cnf中修改配置文件:                       

                   [client]
                   default-character-set=utf8
                   [mysql]
                   default-character-set=utf8
 
                   [mysqld]
                   datadir=/var/lib/mysql
                   socket=/var/lib/mysql/mysql.sock
                   user=mysql
                   #Disabling symbolic-links is recommended to prevent assorted security risks
                   symbolic-links=0
 
                   collation-server= utf8_unicode_ci
                   init-connect='SETNAMES utf8'
                   character-set-server= utf8
                   lower_case_table_names=1    #数据库表明大小写忽略
 
                   log-bin=mysql-bin
                   binlog-format=mixed
 
                   #修改接收数据包大小
                   max_allowed_packet= 20M
                   #打开慢查询日志
                   long_query_time= 1
                   log-queries-not-using-indexes
                   log-slow-admin-statements
                   log-slow-queries= /var/lib/mysql/slow-queries.log

2.2          忘记root密码

1.   首先关闭mysql,然后使用不验证权限的方式启动:

                   mysqld_safe--skip-grant-tables &

2.   直接shell中输入:mysql,进入后对root授权:

              updatemysql.user set password=PASSWORD('root') where User='root';
              flush privileges;

3.   重启mysql服务

2.3          临时关闭外键约束

                   SETFOREIGN_KEY_CHECKS=0;

2.4          清空表里面的数据

                   truncate table tables_name;

2.5          shell中使用mysql语句

        

mysql-uroot -proot -e 'use qhfax;show tables'

2.6          shell查询每个表的语句有多少

#!/bin/sh
 
for i in `mysql -uroot -proot -e 'useqhfax;show tables'`    
do
         COUNT=`mysql-uroot -proot -e "select count(*) from $i"` 
         echo"$i $COUNT"
done

        

        

2.7     mysql重启失败-操作

         检查selinux是否启动,如果启动数据不再mysql权限内无法创建 

2.8          主从同步

主:

1.   配置主服务器打开binlog

vi /etc/my.cn
server-id=1 #给服务器起一个独特的ID 
log-bin=mysql-bin #申明2进制日志的文件为mysql-bin.xxx
binlog-format=mixed  #二进制格式  mixed[由系统根据语句决定]/row[2进制记录磁盘变化]/statement[2进制记录执行语句]

授权:

grant replication client,replication slaveon *.*
  to'repl'@'192.168.85.%' identified by 'repl';
flush privileges;

2.   配置从服务器

vi /etc/my.cnf
server-id=2 
log-bin=mysql-bin
binlog-format=mixed
relay-log=mysql-relay #从服务器自己产生日志

3.   授权从服务器

change master to
master_host='192.168.85.111',
master_user='repl',
master_password='repl',
master_log_file='mysql-bin.000001',
master_log_pos=98;
 
start slave;
        
show master status;
show slave status;    
        
reset master;
reset slave;

2.9     mysql权限

2.9.1     授权实例

create temporary tables 创建零时表权限
show view 查看视图

        

本文实例,运行于 MySQL5.0 及以上版本。

MySQL 赋予用户权限命令的简单格式可概括为:

grant 权限 on 数据库对象 to 用户

1.   grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to common_user@'%'
grant insert on testdb.* to common_user@'%'
grant update on testdb.* to common_user@'%'
grant delete on testdb.* to common_user@'%'

或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete ontestdb.* to common_user@'%'

2.   grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。

grant 创建、修改、删除 MySQL 数据表结构权限。

grant create on testdb.* todeveloper@'192.168.0.%';
grant alter on testdb.* to developer@'192.168.0.%';
grant drop  on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 外键权限。

grant references on testdb.* todeveloper@'192.168.0.%'

grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.*to developer@'192.168.0.%';

grant 操作 MySQL 索引权限。

grant index on  testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 视图、查看视图源代码 权限。

grant create view on testdb.* todeveloper@'192.168.0.%';
grant show  view on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 存储过程、函数 权限。

grant create routine on testdb.* to developer@'192.168.0.%';  -- now, can show procedure status
grant alter routine on testdb.* to developer@'192.168.0.%';  -- now, you can drop a procedure
grant execute        on testdb.* to developer@'192.168.0.%';

3.   grant 普通 DBA 管理某个 MySQL 数据库的权限。

grant all privileges on testdb todba@'localhost'

其中,关键字 “privileges” 可以省略。

4.   grant 高级 DBA 管理 MySQL 中所有数据库的权限。

grant all on *.* to dba@'localhost'

5.   MySQL grant 权限,分别可以作用在多个层次上。

6.   grant 作用在整个 MySQL 服务器上:

grant select on *.* to dba@localhost; --dba 可以查询 MySQL 中所有数据库中的表。
grant all   on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库
1)        grant 作用在单个数据库上:
grant select on testdb.* to dba@localhost;-- dba 可以查询 testdb 中的表。
 
2)       grant 作用在单个数据表上:
grant select, insert, update, delete ontestdb.orders to dba@localhost;
 
3)        grant 作用在表中的列上:
grant select(id, se, rank) ontestdb.apache_log to dba@localhost;
 
4)       grant 作用在存储过程、函数上:
grant execute on procedure testdb.pr_add to'dba'@'localhost'
grant execute on function  testdb.fn_add to 'dba'@'localhost'

7.   查看 MySQL 用户权限

查看当前用户(自己)权限:
show grants;
 
查看其他 MySQL 用户权限:
show grants for dba@localhost;

8.   撤销已经赋予给 MySQL 用户权限的权限。

revoke 跟 grant 的语法差不多,只需要把关键字“to”换成 “from” 即可:

grant all on *.* to   dba@localhost;
revoke all on *.* from dba@localhost;

9.   MySQL grant、revoke 用户权限注意事项

1)       1. grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“

grant select on testdb.* to dba@localhostwith grant option;

这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。     

        

#查看grant添加的用户:selectuser,host from mysql.user;

#删除用户:

mysql> drop user"tongor"@localhost;

        

2.10  mysql存储过程权限

即时赋予一个用户,执行存储过程的权限,普通用户执行非他自己创建的存储过程还是会失败

execute command denied to user'test_ryd'@'%' for routine 'test_04.generate_serialno'
The user specified as a definer('user_admin'@'%') does not exist

但是提示存储过程定义中的definer不存在,原来仅仅是连接到MySQL服务器的用户具有执行存储过程的权限是远远不够的,最终要通过存储过程定义中指定的definer来执行存储过程。

3      mysql排错

3.1     msyql.proc is wrong

         解决:mysql_upgrade -u root -p

         知识:mysql_upgrade升级授权表

        

        

3.2     ERROR 2006 (HY000) at line1: MySQL server has gone away

         解决:SQL语句过长,建议改语句

         知识:insert执行语句过长造成的

        

3.3     Cannot load frommysql.proc. The table is probably corrupted

【错误过程】:MySQL从5.1升级至5.5后在调用存储过程时报出“Cannotload from mysql.proc. The table is probably corrupted。”
【造成原因】:MySQL升级完成后未对相关数据库执行升级.
【解决办法】:在命令行中执行mysql_upgrade-uroot -p 即可~

3.4     mysql max_allowed_packet

         showVARIABLES like '%max_allowed_packet%';
        
         可以编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改。
    max_allowed_packet = 20M
          
          set global max_allowed_packet = 2*1024*1024*10
          show VARIABLES like '%max_allowed_packet%';

3.5     Mysql Got error 28 fromstorage engine错误:磁盘临时空间不够导致

3.6          cloumn count of mysql.proc

         mysql_upgrade-u root -proot --force

        

3.7          Grant的时候报错的解决:Accessdenied for user 'root'@'localhost' (using password: YES)

     解决:mysql_upgrade -uroot -proot--force
     知识点:低版本mysql向高版本迁移的时候,存储过程问题

3.8          主从不同步

方法一:忽略错误后,继续同步
该方法适用于主从库数据相差不大,或者要求数据可以不完全统一的情况,数据要求不严格的情况
解决:
stop slave;
#表示跳过一步错误,后面的数字可变
set global sql_slave_skip_counter =1;
start slave;
之后再用mysql>show slave status\G 查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
ok,现在主从同步状态正常了。。。

猜你喜欢

转载自blog.csdn.net/u013958257/article/details/88059856