mysql初级运维使用技巧


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

1      mysql最基础

1.1     mysql安装

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

   yum -y install mysql mysql-server mysql-devel

   注意,若是是centos库和有些国内yum源的mysql版本都比较低,若是想要安装高版本mysql,须要换源,具体操做以下:sql

         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服务设置为开机自启动:shell

 

         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'@'%';

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

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权限内没法建立 windows

2.8          主从同步

主:centos

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进制记录执行语句]

受权:bash

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 grantrevoke 用户权限注意事项

 

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,如今主从同步状态正常了。。。
相关文章
相关标签/搜索