设置更改root密码 链接mysql 及mysql经常使用命令

6月19日任务

13.1 设置更改root密码
13.2 链接mysql
13.3 mysql经常使用命令
扩展 
mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置详解: http://blog.linuxeye.com/379.html
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同窗分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.htmlhtml

设置更改root密码目录概要

  • /usr/local/mysql/bin/mysql -uroot
  • 更改环境变量PATH,增长mysql绝对路径
  • mysqladmin -uroot password '123456'
  • mysql -uroot -p123456
  • 密码重置
  • vi /etc/my.cnf//增长skip-grant
  • 重启mysql服务 /etc/init.d/mysqld restart
  • mysql -uroot
  • use mysql;
  • update user set password=password('aminglinux') where user='root';

设置更改root密码

  • root用户是mysql的超级管理员用户,和linux系统的root用户相似,不过和Linux的不同
  • 默认mysql的 root 用户密码是空的,直接就能够链接上去,不须要输入密码,可是不安全,因此就须要设置一个密码
  • 为了方便使用mysql服务,将mysql目录加入到环境变量里
  1. 打开系统,查看mysql是否启动 ps aux|grep mysql
  2. 如果没有启动mysql的话,将mysql启动起来 service mysqld start 
  3. 在启动mysql后,使用mysql -uroot命令,可是mysql命令会提示不存在,由于安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,因此它会报错 ,若想要这个命令直接运行,须要把PATH作一个更改
[root@yong-01 ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@yong-01 ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@yong-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 
[root@yong-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/
  • 这时再来使用mysql -uroot命令就会发现可使用了 退出mysql输入 quit;或者exit; 便可
[root@yong-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
  1. 若想要变量永久生效,还须要将export PATH=$PATH:/usr/local/mysql/bin/  放入到 /etc/profile 最后面
  2. 假设如果没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,由于变量尚未生效,想要这个变量生效,在配置文件中加入命令后,还须要执行source /etc/profile 命令,从新加载
  3. 通常是使用mysql -uroot -p命令  -p,表示指定密码
  4. 密码为空的时候,直接回车就可进入到mysql,并能够在其中操做一些mysql的一些行为
  5. 退出mysql,输入 quit 便可
  6. 设置mysql密码,命令为mysqladmin -uroot passwd '123456' 在 ' ' 为密码
[root@yong-01 ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.
  • 在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你 你如今密码在当前命令行显示出来了,这样不太安全
  • 这时在想直接登陆mysql,就会提示须要输入密码了
[root@yong-01 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  • 在使用-p,并输入密码就能够正常的进入到mysql命令行了
[root@yong-01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

知道mysql的root密码,去更改密码

  1. 如果这时知道mysql密码,去修改mysql密码,看到输出的提示信息不用去理会
  2. 格式
    • mysqladmin -uroot -p'123456' password '654321'
[root@yong-01 ~]# mysqladmin -uroot -p'123456' password '654321'
Warning: Using a password on the command line interface can be insecure.
  • 指定新密码去登陆,固然也能够不明文指定密码,知道-p回车,输入密码登陆也行
[root@yong-01 ~]# mysql -uroot -p'654321'
Warning: Using a password on the command line interface can be insecure.
  • 在明文指定密码的时候,密码能够加单引号,也能够不加单引号,建议加上单引号,防止密码有特殊符号的存在——>(如果不加单引号,而密码中又有特殊符号,就有可能会不识别)

不知道mysql的root密码,去更改密码

  1. 在不知道mysql的root用户密码的时候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
  2. skip-grant ,表示忽略受权,也就是说操做mysql的时候不须要用户名和密码了,能直接登陆
  3. 在更改配置文件后,重启mysql服务 service mysqld restart
  4. 这时候在输入mysql -uroot ,会发现直接进入mysql,而不须要密码了
[root@yong-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
  • 在登陆进mysql后,还须要更改一个表,由于用户名和密码是存在于一个mysql库里面的,使用 use mysql; 切换库,在切换到mysql库里面,而后去更改一个存用户名密码的user表
  • use mysql; 切换库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
  1. 查看user表,输入select * from user; 命令,会看到输出乱七八糟的乱码,里面存放的就是用户名和密码,还有权限和受权等信息
  2. 查看password表,会看到密码都是加密的
mysql> select password from user where user='root';
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
4 rows in set (0.05 sec)
  • 更改user表,使用update user set password=password('111111') where user='root'; 命令
  • update user set password=password('111111') where user='root';

 

密码字段       函数 //用于加密密码    高亮部分:为条件语句
mysql> update user set password=password('111111') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
  1. 提示说4行修改完毕,即便有些行是空的
  2. 这样密码就更改为功了,输入quit退出数据库便可
  3. 再去 /etc/my.cnf 配置文件中删除免受权配置,即删除skip-grant——>如果不删除,那么以后全部的用户都不须要输入密码,就能够登陆进去,这样安全性过低
  4. 重启mysql服务 service mysqld restart
  5. 重启完以后,再用新的密码测试下,会看到新的密码能够登陆 mysql -uroot -p'111111'
  6. 这样就是成功更改mysql密码

链接mysql

  • 本地链接——>即便没有指定,但默认使用sock链接,使用/tmp/mysql.sock链接
    • mysql -uroot -p123456 //输入用户名和密码链接本机
  • 使用ip端口链接远程机器
    • mysql -uroot -p'111111' -h[远程mysql主机IP] -P[端口]
[root@yong-01 ~]# mysql -uroot -p'111111' -h127.0.0.1 -P3306
  • 使用sock方式链接(只适合在本机使用)如为指定IP就用sock访问
    •  mysql -uroot -p'111111' -S/tmp/mysql.sock
[root@yong-01 ~]# mysql -uroot -p'111111' -S/tmp/mysql.sock
  • 链接mysql以后执行命令(经常使用于shell脚本)
  • mysql -uroot -p123456 -e “show databases”
  • show databases //列出全部数据库
[root@yong-01 ~]# mysql -uroot -p'111111' -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

mysql经常使用命令

  • 查询库 show databases;
  • 切换库 use mysql;
  • 查看库里的表 show tables;
  • 查看表里的字段 desc tb_name;
  • 查看建表语句 show create table tb_name\G;
  • 查看当前用户 select user();
  • 查看当前使用的数据库 select database();
  • 建立库 create database db1;
  • 建立表 use db1; create table t1(id int(4), name char(40));
  • 查看当前数据库版本 select version();
  • 查看数据库状态 show status;
  • 查看各参数 show variables; show variables like 'max_connect%';
  • 修改参数 set global max_connect_errors=1000;
  • 查看队列 show processlist; show full processlist;

建立库

  • 建立库 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
  • 查看db1库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
  • 切换到db1库下面去
mysql> use db1;
Database changed
  • 建立表 create table t1(id int(4), name char(40));——>定义 id 和 name ,并用反引号括起来
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.03 sec)
  • 查看建立的表t1,默认使用的是InnoDB 引擎
mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified
  1. 在数据库里面取消命令,即在命令的最前面加一个 # 号就不会生效了
  2. 删除表 drop table t1;
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)
  • 建立t1表
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.06 sec)
  • 查看表,会看到变成了utf8
mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified
  • 查看当前数据库版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)
  • 查看数据库状态 show status; 它会把经常使用的数据都列出来
  • 查看各参数 show variables; show variables like 'max_connect%';      //  mysql下 % 为通配符
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)
  • 修改参数 set global max_connect_errors=1000; ——>仅在内存中生效
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)
  • 若想重启依然生效,那须要修改配置文件/etc/my.cnf

查看队列

  • show processlist; //查看库的情况,好比,那些用户在连,作了些什么操做,是否锁表
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  5 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
  • show full processlist; //查看到的对列,最后一个会很是完成的显示出来
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

mysql>
相关文章
相关标签/搜索