码迷,mamicode.com
首页 > 数据库 > 详细

第19章   简单的MySQL操作

时间:2016-11-15 01:48:29      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:mysql 操作

1. 如何更改系统环境变量PATH?
vim /etc/profile.d/path.sh  加入

  #!/bin/bash

  export PATH=$PATH:/usr/local/mysql/bin:/usr/local/apache2/bin

2. 默认mysql安装好后,并没有root密码,如何给root设置一个密码?
 mysqladmin -uroot password  ‘newpass‘   
3. 如何更改root密码?
  mysqladmin -uroot -poldpasswd password ‘newpasswd‘

4. 如何连接远程的mysql服务器?
mysql -uusername -ppass -hhostip -Pport   

username 用户名  pass密码  hostip 另一台机的ip  -P 连接的端口

5. 如何查看当前登陆mysql的账户?
  select user();
6. 在mysql命令行下,怎么切换某个库?
  use databasename;  或 use mysql;或  use discuz;    
7. 如何查看一个表都有哪些字段?
   desc tablename;
8. 如何查看某个表使用的是哪种数据库引擎?
   show create table\G;

9. 如何查看当前数据库有哪些队列?
  show processlist;
10. 当有很多队列时,如何查看有哪些慢查询?
看慢查询日志,慢查询日志在/etc/my.cnf中设置方法是增加:
log_slow_queries =  /data/mysql/slow.log
long_query_time = 1  //查询时间超过1s会记录日志

查看日志文件  cat  /data/mysql/slow.log

 命令   show global status like "%slow%";

11. 如何查看当前mysql的参数值?
  show variables;

12. 如何不重启mysql服务,更改某个参数?
 set global xxx = xxx; 比如
set global wait_timeout = 10; 或者  set global max_connect_errors=1000; 

13. 用什么工具备份数据库?请区分myisam引擎和innodb引擎两种存储引擎的备份。
mysqldump 备份数据库,mysqldump可以备份两种引擎的数据。但是innodb引擎的数据使用xtrabackup工具更快

14. 简单描述myisam和innodb引擎的区别。
innodb不支持fulltext类型索引;
innodb不保存表的行数;
myisam的数据直接存在系统的文件中,而innodb的数据库会事先创建一个数据表空间文件,然后再从这个表空间文件中存数据;
myisam不提供事务支持,InnoDB提供事务支持事务,外部键等高级 数据库功能;
myisam的锁是对整个表锁定,innodb是行锁;

15. 如果你的mysql服务启动不了,而当前终端又没有报错,你如何做?
查看mysql的日志,日志默认在datadir下,以hostname为名字的.err文件

16. 要备份的库字符集是gbk的字符集,为了避免出现乱码的情况,如何在备份和还原的时候指定字符集为gbk?
备份指定 mysqldump --default-character-set=gbk
恢复指定  mysql --default-character-set=gbk

17. 错误日志中,如果出现提示说某个表损坏需要修复,你如何修复这个表呢?
repair table tablename;  例如  mysql> repair table discuz.pre_forum_post;

18. 备份myisam引擎的数据库时,我们除了使用mysqldump工具备份外,还可以直接拷贝数据库的源数据(.frm, .MYD, .MYI三种格式的数据),其中哪一个文件可以不拷贝?若想恢复该文件,如何做?
.MYI的文件可以不拷贝,恢复的时候,需要修复表,但加上 use_frm, 如
repair table tb1 use_frm;

19. 如果mysql的root密码忘记了如何做?
1) 编辑mysql主配置文件 my.cnf  vim /etc/my.cnf  
在[mysqld]字段下添加参数  skip-grant  
2) 重启数据库服务  service mysqld restart
3) 这样就可以进入数据库不用授权了  mysql -uroot 
4) 修改相应用户密码  use mysql;
update user set password=password(‘your password‘) where user=‘root‘;
flush privileges;

5) 修改/etc/my.cnf 去掉 skip-grant , 重启mysql服务

20. 如何更改mysql的普通账户密码?

update user set password=password(‘your password‘) where user=‘username‘;

21. mysql的命令历史文件在哪里,为了安全我们其实是可以做一个小处理,不让mysql的命令历史记录在文档中,请想一想如何利用之前我们学过的知识做到?
~/.mysql_history 
我们可以这样不保存mysql命令历史: cd ~; rm -f .mysql_history; ln -s /dev/null .mysql_history

22. 如何让mysql的监听端口为3307,而不是默认的3306?
vim /etc/my.cnf 
把port            = 3306 改为 port            = 3307
扩展阅读:

mysql 配置参数详解 http://www.lishiming.net/thread-87-1-1.html

mysql 5.0 与 5.1 记录慢查询日志的区别  http://www.lishiming.net/thread-374-1-1.html

myisamchk 修复表  http://www.lishiming.net/thread-256-1-1.html

同一台MySQL服务器启动多个端口  http://www.lishiming.net/thread-63-1-1.html

mysqld_multi stop 不能停掉mysql  http://www.lishiming.net/thread-624-1-1.html

mysql 使用innodb引擎 http://www.lishiming.net/thread-955-1-1.html

mysql innodb引擎让为每一个表分配一个表空间 http://www.lishiming.net/thread-5498-1-1.html

如何查看mysql的命令历史  http://www.lishiming.net/thread-1022-1-1.html

mysql-5.1默认不支持innodb引擎,默认不支持gbk字符集  http://www.lishiming.net/thread-1229-1-1.html

mysql 在指定IP上启动端口  http://www.lishiming.net/thread-208-1-1.html

mysql 忘记root密码怎么办 http://www.lishiming.net/thread-252-1-1.html

windows mysql root 密码忘记怎么办 http://www.lishiming.net/thread-1014-1-1.html 

mysql删除某个表前100条数据 http://www.lishiming.net/thread-5452-1-1.html

mysql常用授权  http://www.lishiming.net/thread-88-1-1.html

mysqldump 备份和恢复指定表   http://www.lishiming.net/thread-131-1-1.html

mysql 5.0/5.1版本修改普通用户的密码   http://www.lishiming.net/thread-892-1-1.html

mysql显示SQL语句执行时间 http://www.lishiming.net/thread-214-1-1.html

mysql 复制一个表,复制一个表结构的sql 语句  http://www.lishiming.net/thread-219-1-1.html

mysql innodb引擎让为每一个表分配一个表空间 http://www.lishiming.net/thread-5498-1-1.html

使用xtrabackup备份innodb引擎的数据库    http://www.lishiming.net/thread-956-1-1.html

mysql5.1所支持的几种存储引擎 http://www.lishiming.net/thread-5501-1-1.html

mysql 导入超级大的sql文件时mysql服务重启 http://www.lishiming.net/thread-5395-1-1.html

mysql myisam和innodb引擎对比 http://www.lishiming.net/thread-97-1-1.html

第19章   简单的MySQL操作

标签:mysql 操作

原文地址:http://helins.blog.51cto.com/11777116/1872739

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!