整理了一下,工做中用到的最高的关于mysql的一些命令和使用技巧,分享给刚接触mysql的小伙伴么。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. shell命令行:mysqladmin -uroot -ppassword 'new_password' 2. mysql命令行:upload mysql.user set password=password("new_passwd")where user="root" and host="localhost";
1. 帐号受权:grant all privileges on *.* to user_name@'%' identified by '1234'; 2. 权限回收:revoke GRANT ALL PRIVILEGESON *.* from 'user_name'@'%';
这个只属于新手用的命令,更详细的会在下文中详细介绍apache
showvariables like "%char%";
SETcharacter_set_client='utf8'; SETcharacter_set_connection='utf8'; SETcharacter_set_results='utf8';
[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
mysqld_safe--skip-grant-tables &
updatemysql.user set password=PASSWORD('root') where User='root'; flush privileges;
SETFOREIGN_KEY_CHECKS=0;
truncate table tables_name;
mysql-uroot -proot -e 'use qhfax;show tables'
#!/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
检查selinux是否启动,若是启动数据再也不mysql权限内没法建立 windows
主:centos
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;
vi /etc/my.cnf server-id=2 log-bin=mysql-bin binlog-format=mixed relay-log=mysql-relay #从服务器本身产生日志
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;
create temporary tables 建立零时表权限 show view 查看视图
本文实例,运行于 MySQL5.0 及以上版本。服务器
MySQL 赋予用户权限命令的简单格式可归纳为:
grant 权限 on 数据库对象 to 用户
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@'%'
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.%';
grant all privileges on testdb todba@'localhost'
其中,关键字 “privileges” 能够省略。
grant all on *.* to dba@'localhost'
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'
查看当前用户(本身)权限: show grants; 查看其余 MySQL 用户权限: show grants for dba@localhost;
revoke 跟 grant 的语法差很少,只须要把关键字“to”换成 “from” 便可:
grant all on *.* to dba@localhost; revoke all on *.* from dba@localhost;
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;
即时赋予一个用户,执行存储过程的权限,普通用户执行非他本身建立的存储过程仍是会失败
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来执行存储过程。
解决:mysql_upgrade -u root -p
知识:mysql_upgrade升级受权表
解决:SQL语句过长,建议改语句
知识:insert执行语句过长形成的
【错误过程】:MySQL从5.1升级至5.5后在调用存储过程时报出“Cannotload from mysql.proc. The table is probably corrupted。” 【形成缘由】:MySQL升级完成后未对相关数据库执行升级. 【解决办法】:在命令行中执行mysql_upgrade-uroot -p 便可~
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%';
mysql_upgrade-u root -proot --force
解决:mysql_upgrade -uroot -proot--force 知识点:低版本mysql向高版本迁移的时候,存储过程问题
方法一:忽略错误后,继续同步 该方法适用于主从库数据相差不大,或者要求数据能够不彻底统一的状况,数据要求不严格的状况 解决: 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,如今主从同步状态正常了。。。