十三周一次课

第13章 MySQL常用操作

MySQL版本 5.6.35

13.1 设置、更改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';

首先查看MySQL服务有没有启动,命令:ps aux |grep mysql

[root@localhost ~]#ps aux |grep mysql

root       3545    0.0   0.0  56464 154 pts/0    R+   15.53    0.00 grep --color=auto mysql

如果没有启动,启动命令:/etc/init.d/mysqld start

[root@localhost ~]#Starting MySQL .   SUCCESS!

[root@localhost ~]#!ps

[root@localhost ~]#mysql -uroot

-bash: mysql:未找到命令                      #这个命令没有在环境变量path里面,所以报错了。

[root@localhost ~]#ls /usr/local/mysql/bin/mysql             #查看命令所在的目录,

[root@localhost ~]#echo $PATH                         #查看环境变量

首次直接使用mysql会提示‘该命令不存在’,原因是还没有将该命令加入环境变量,如果要使用该命令,需要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量:

[root@localhost ~]# exprt PATH=$PATH:/usr/local/mysql/bin/

至此,mysql命令路径暂时加入环境变量,系统重启后该变量会失效,若要永久生效,需要将其加入环境变量配置文件:

[root@localhost  ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/

刷新配置文件(否则不生效):
[root@localhost  ~]# source /etc/profile

设置 & 更改密码

首次登陆mysql,root用户没有密码,直接登录:

[root@localhost  ~]# mysql -uroot
#-u:=user,指定用户名
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> quit
#退出

说明: 登录mysql之后可以进行与mysql相关的一些操作,但是设置mysql用户的密码需要执行以下操作!

设置密码

[root@localhost ~]# mysqladmin -uroot password '123456'  

再次登录:
[root@localhost ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

说明: 设置密码后直接登录会报错(ERROR),需要输入密码登录。

[root@localhost  ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql>

注: -p=passwd,使用密码登录,在此可以将密码直接输入在命令行(跟在-p后面,不加空格:-p'123456'<此处单引号可以不加,但是当密码中有特殊符号时必须加,所以在命令行输入密码时养成习惯:加单引号>),也可以不在命令行输入,只跟-p选项,然后根据提示信息:“Enter password”,输入密码进行登录(此方法不会暴露用户密码,安全)。

更改密码

  • 当知道用户密码时,进行密码更改:
[root@localhost  ~]# mysqladmin -uroot -p'123456' password '1234567'

[root@adailinux ~]# mysql -uroot -p'1234567'
Welcome to the MySQL monitor.
mysql>

更改成功!

  • 忘记密码时,进行密码更改:
先编辑mysql配置文件:
[root@localhost  ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授权!
datadir=/data/mysql
socket=/tmp/mysql.sock

重启mysql服务:
[root@adlocalhost  ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS! 
Starting MySQL..................... SUCCESS! 

说明: 完成该操作之后就可以任意登录mysql了(无需密码),所以此时mysql安全性很差,平时配置文件中一定不要添加该参数!!!

[root@localhost  ~]# mysql -uroot
Welcome to the MySQL monitor.  
mysql> use mysql;
#切换mysql库
Database changed
mysql> select * from user\G;
#查看用户的表信息,该表中存放的是用户相关信息(密码、授权…)
#G选项的作用是使输出信息有序显示,不加该选项,显示内容会很乱  
mysql> select password from user;
#查看用户密码,显示结果Wie加密字符串!  
mysql> update user set password=password('123456') where user='root';
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4  Changed: 4  Warnings: 0
#将密码更改为‘123456’
mysql> quit
Bye

密码更改成功!

恢复配置文件:
[root@localhost  ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重启mysql服务:
[root@adailinux ~]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL........... SUCCESS! 

登录:
[root@localhost  ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> quit
Bye

Finished!
步骤: vim /etc/my.cnf-->添加skip-grant-->mysql restart-->登录-->use mysql-->update user set password=...-->vim /etc/my.cnf-->删除skip-grant-->mysql restart。

13.2 连接mysql(本地、远程)

远程连接:使用IP/port连接

[root@localhost  ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit
Bye

注: -h:=host,指定IP;-P:=port,指定端口。

本地连接:使用socket连接

[root@localhost  ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit
Bye

注: -S:=socket,指定socket。此方法只适用于本地连接,等同于“mysql -uroot -p123456”。

显示所有数据库

[root@localhost  ~]# mysql -uroot -p'123456' -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

注: 该方法使用于shell脚本中。

13.3 MySQL常用命令

查看库信息:

mark

以下命令需要在切换库(use mysql)之后执行:

mark

编辑库:

mark

注: 以上命令均需要在mysql下执行;在mysql中每行命令末尾加上分号,表示该行命令执行结束。 tb_name即table name()表名。

示例:

[root@localhost  mysql]# mysql -uroot -p'123456'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

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> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| time_zone                 |
| time_zone_leap_second     |
+---------------------------+
28 rows in set (0.00 sec)

mysql> desc time_zone;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| Time_zone_id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Use_leap_seconds | enum('Y','N')    | NO   |     | N       |                |
+------------------+------------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)

mysql> show create table time_zone\G;
#G=grep筛选文字内容,规律显示出来
*************************** 1. row ***************************
       Table: time_zone
Create Table: CREATE TABLE `time_zone` (
  `Time_zone_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Use_leap_seconds` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Time_zone_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Time zones'
1 row in set (0.03 sec)

ERROR: 
No query specified

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.07 sec)

mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> select * from user\G;
创建库:
mysql> create database db1;
Query OK, 1 row affected (0.02 sec)

创建表:
mysql> use db1;  
#先切换到指定库下
Database changed
mysql> create table t1(`id` int(4),`name` char(40));
#括号中是定义字段及字段格式,使用反引号引起来
Query OK, 0 rows affected (1.51 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.06 sec)

mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
+-----------------------------------------------+-------------+

mysql> show variables\G;

mysql> show variables like 'max_connect%'\G;
#like表示匹配;%是通配符

更改参数:
mysql> set global max_connect_errors=110;
Query OK, 0 rows affected (0.04 sec)
#在此只是临时更改,如果要永久更改,需要编辑配置文件

查看队列:
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.01 sec)

mysql> drop table t1;
Query OK, 0 rows affected (0.32 sec)

mysql> drop database db1;
Query OK, 0 rows affected (0.10 sec)

扩展:MySQL5.7之更改root密码

与MySQL 5.6版本不同,在安装MySQL 5.7过程中(初始化)会自动生成root用户密码(随机),那么在安装完成后如何更改root用户密码?步骤如下:

查看默认密码

[root@localhost  mysql]# cat /root/.mysql_secret
# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcFP

更改root密码:已知默认密码

使用默认密码登录:
[root@localhost  mysql]# /usr/local/mysql/bin/mysql -uroot -p'3A)2DdJLkcFP'
Welcome to the MySQL monitor.  
Your MySQL connection id is 3
Server version: 5.7.17

设置新密码:
方法1:
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
方法2:
mysql> SET PASSWORD FOR 'root'@localhost = PASSWORD('123456');
mysql> quit
Bye

Finished!

更改root密码:不知道默认密码

编辑配置文件:
[root@localhost  mysql]# vi /etc/my.cnf

[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增加参数:skip-grant-tables

重启:  
[root@localhost  mysql]# /etc/init.d/mysqld restart

登录:此时不需要密码
[root@localhost  mysql]# /usr/local/mysql/bin/mysql -uroot 

更改密码:
mysql> update user set authentication_string=password('12456') where user='root';
mysql>quit

[root@localhost  mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重启: 
[root@localhost  mysql]# /etc/init.d/mysqld restart

Finished!

常见问题:

1、环境变量  PATH=$PATH:/usr/local/mysql/bin/    和export PATH=$PATH:/usr/local/mysql/bin/   有什么区别?

答:加上 export后,意味着这个PATH变量可以在当前shell以及子shell中生效了。

mysql问题

  1. mysql的group_concat()函数不能排序;
  2. mysql查询不能直接获取记录的行号;
  3. mysql不支持full join
  4. 使用不等于(!= 或者 <>)的时候,MySQL 无法使用索引;
  5. 当between and字段类型是时间类型时,mysql不能查询到后边界
  6. mysql查询每次只能使用一个索引,所以为了提高查询效率,有时需要建立复合索引

    7.无滚动游标,函数不可以返回虚拟表


扩展 
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.html

猜你喜欢

转载自my.oschina.net/u/3803405/blog/1812147
今日推荐