mysql使用密码连接数据库_设置mysql用户密码、远程连接数据库、常用命令

第13章 MySQL常用操作

MySQL版本 5.6.35

13.1 设置、更改root用户密码

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

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

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

[root@1 ~]# vim /etc/profile

……

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

刷新配置文件(否则不生效):

[root@1 ~]# source /etc/profile

设置 & 更改密码

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

[root@1 ~]# mysql -uroot

#-u:=user,指定用户名

Welcome to the MySQL monitor. Commands end with ; or \g.

……

mysql> quit

#退出

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

设置密码

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

再次登录:

[root@1 ~]# mysql -uroot

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

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

[root@1 ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.

mysql>

注: -p=passwd,使用密码登录,在此可以将密码直接输入在命令行(跟在-p后面,不加空格:-p'123456'),也可以不在命令行输入,只跟-p选项,然后根据提示信息:“Enter password”,输入密码进行登录(此方法不会暴露用户密码,安全)。

更改密码

当知道用户密码时,进行密码更改:

[root@1 ~]# mysqladmin -uroot -p'123456' password '1234567'

[root@1 ~]# mysql -uroot -p'1234567'

Welcome to the MySQL monitor.

mysql>

更改成功!

忘记密码时,进行密码更改:

先编辑mysql配置文件:

[root@1 ~]# vim /etc/my.cnf

[mysqld]

skip-grant

#忽略授权!

datadir=/data/mysql

socket=/tmp/mysql.sock

重启mysql服务:

[root@1 ~]# /etc/init.d/mysqld restart

Shutting down MySQL... SUCCESS!

Starting MySQL..................... SUCCESS!

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

[root@1 ~]# 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@1 ~]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/tmp/mysql.sock

重启mysql服务:

[root@1 ~]# /etc/init.d/mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL........... SUCCESS!

登录:

[root@1 ~]# 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@1 ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306

Welcome to the MySQL monitor.

mysql> quit

Bye

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

本地连接:使用socket连接

[root@1 ~]# mysql -uroot -p123456 -S/tmp/mysql.sock

Welcome to the MySQL monitor.

mysql> quit

Bye

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

显示所有数据库

[root@1 ~]# 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常用命令

查看库信息:

•查询库 show databases;

• 切换库 use mysql;

• 查看库里的表 show tables;

• 查看表里的字段 desc tb_name;

• 查看建表语句 show create table tb_name\G;

• 查看当前用户 select user();

• 查看当前使用的数据库 select databsase();

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

示例:

[root@1 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@1 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@1 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@1 mysql]# vi /etc/my.cnf

[mysqld]

skip-grant-tables

datadir=/data/mysql

socket=/tmp/mysql.sock

#增加参数:skip-grant-tables

重启:

[root@1 mysql]# /etc/init.d/mysqld restart

登录:此时不需要密码

[root@1 mysql]# /usr/local/mysql/bin/mysql -uroot

更改密码:

mysql> update user set authentication_string=password('12456') where user='root';

mysql>quit

[root@1 mysql]# vi /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/tmp/mysql.sock

重启:

[root@1 mysql]# /etc/init.d/mysqld restart

Finished!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值