Grant select,insert,update,deleteon webmysql.* to user@”10.1.1.%”> 从库:
Grant select onwebmysql.* to user@”10.1.1.%”>
1. 生产环境读写分离账户设置:
主库(提供写服务):webmysql(数据库) user(账户) ip:10.1.1.21 port 3306
从库(提供读服务):webmysql(数据库) user(账户) ip:10.1.1.22 port 3306
2. 数据库备份。
从库备份要打开binlog,备份包括全备和binlog增量备份。
3. 创建数据库指定字符集:
GBK:create database darren DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
UTF8: CREATE DATABASE darren DEFAULT CHARACTER SET utf8 COLLATEutf8_general_ci;
4. 查看数据库的用户:
selectuser,host from mysql.user;
drop user'root'@'localhost'; 删除root用户
deletefrom mysql.user 删除全部用户
删除用户时有大写,用drop是删不掉的,需要用delete
deletefrom mysql.user where user=’Root’ and host=’localhost’;
grant allprivileges on *.* to system@’localhost’> 另一种授权的方法:
先创建用户:create user Darren@’localhost’identified by ‘password’;
再授权:grant all on *.* to Darren@’localhost’;
收回权限:
REVOKEdelete on wordpress.* FROM ‘wordpress’@’localhost’;
创建表:
createtable test (id int(4) not null primary key auto_increment,name char(20) notnull);
desc 表名 :查看表结构
showcloumns from 表名
showcreate table mysql.表名\G
show indexfrom 表名 :查看索引 建表后添加索引:
altertable 表名 add index 索引名(name)
createindex 索引名 on 表名(name:列)--不能创建主键索引
Show grantfor system@‘localhost’ (查看system账号的权限。)
Selectuser();当前用户。
Showcreate database mysql; 查看创建mysql数据库的字符集类型。
Show characterset; 查看所有的字符集。
select *from darren.test 查看数据库的表。
showprocess list 查看mysql当前的线程数(用户执行的mysql语句)
注:语句如果停留太久说明数据库有问题,需要优化。显示全部的话用show full processlist
mysql –uroot –p ‘password’ –e “show proesslist;” | grep xxx或者>xxx.log
注:-e的好处是可以过滤一些不需要的东西
show variables 显示数据库中的配置,包括my.cnf中的配置是否生效。 show global status 查看数据库当前的状态。-包括在线查询人数,缓存用量,插入数据的人数,删除数据的人数。
set globalserver_id=2 不重启数据使修改数据库参数,让其生效。如果想重启还生效需要改配置参数。
key_buffer_size=16M 远程连接数据库:
mysql –uroot –p ‘password’ –P 3307 –h 10.1.1.21 查看mysql现在已提供什么存储引擎:
mysql> show engines; 看你的mysql当前默认的存储引擎:
mysql> show variables like '%storage_engine%';
你要看某个表用了什么引擎(在显示结果里参数engine后面的就表示该表当前用的存储引擎):
mysql> show create table 表名;
5. 刷新mysql系统权限
flushprivileges;
showcreate databases oldboy \G;
6. 数据库的备份
Mysqldump–u root –p ‘password’ oldboy(数据库名)>/backup/$(date+%F).sql
恢复:注意如果没有要恢复的数据库要先创建数据库
Createdatabase oldboy
Mysql –uroot –p ‘password’ oldboy/backup/$(date +%F).sql
加-B恢复数据时不需要重新创建数据库。 压缩备份:
Mysql –uroot –p ‘password’ –B oldboy |gzip
select * from student where>
select * from student where> 更新数据:
update student set name='oldboy' where> 删除数据
delete from student where> 在表中增删改字段
alter table student add sex char(4);
later table student add age char(4) after name;
更改表名:
rename table student to teach;
mysql -u root -p'mysqlpassword' /tmp/$(date +%F).sql
EOF