13.1 设置更改root密码 13.2 连接mysql 13.3 mysql常用命令

原创
2018/06/21 16:27
阅读数 201

13.1 设置更改root密码

启动MySQL数据库

[root@linux-10 ~]# /etc/init.d/mysqld start
Starting MySQL SUCCESS! 

由于MySQL的相关命令的所在路径不在系统的环境变量中,因此需要将路径添加至环境变量中

vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/

更新环境变量

[root@linux-10 ~]# source /etc/profile

使用root用户进入MySQL

[root@linux-10 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
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> 

登录时加-u选项可指定登录的用户,-p选项可指定登录用户的密码

mysql -uroot -p'123456'

-p后输入的密码最好用单引号引用,避免密码中存在特殊符号导致系统不识别。

退出MySQL并设置root用户密码

mysql> quit
Bye
[root@linux-10 ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.
//警告的意思是密码被明文显示在屏幕上是不安全的

修改密码(知道原密码)

mysqladmin -uroot -p'旧密码' password '新密码'

重置密码(不知道原密码)

更改MySQL配置文件

vim /etc/my.cnf

skip-grant的作用是忽略授权,意思是操作数据库时可以直接登录,无需密码。

进入数据库修改存放用户密码的user表

use mysql //使用MySQL库
mysql> update user set password=password('12345678') where user='root'; //将root用户密码改为12345678
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

修改数据库配置文件

将skip-grant删除,避免用户可以直接登录数据库,降低风险

vim /etc/my.cnf//删除skip-grant

重启MySQL服务

/etc/init.d/mysqld restart

结果测试

[root@linux-10 ~]# mysql -uroot -p'12345678'
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 3
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> 

MySQL数据库已经可以通过新密码登录。

13.2 连接mysql

连接本机MySQL

mysql -uroot -p123456  //输入用户名和密码
mysql -uroot -p123456 -S/tmp/mysql.sock  //指定本机监听的sock文件

远程连接MySQL 

mysql -uroot -p123456 -h127.0.0.1 -P3306  //需要额外指定IP和端口号

连接并执行一些命令(多用于shell脚本)

mysql -uroot -p123456 -e “show databases”  //登录同时显示出所有的数据库

13.3 mysql常用命令

在MySQL中的操作需要添加分号。

查询库

show databases;

切换库

use mysql;

查看库里的表

show tables;

查看表里的字段

desc tb_name;

注:表里的字段相当于我们日常使用的表的表头。

查看建表语句

show create table tb_name\G;

\G可以让输出的结果更加规整,在执行sql语句时也可以加\G

查看当前用户

select user();

 注:MySQL的命令历史在Linux系统的root目录下的.mysql_history文件存放。

查看当前使用的数据库

select databsase();

创建库

create database 数据库名称;

创建表

use 数据库名称; create table 表名称(`字段名称` int(4), `字段名称` char(40)); 
//字段名称后是每个字段的字段类型

查看当前数据库版本

select version();

查看数据库状态

show status;

查看各参数

show variables;
show variables like 'max_connect%';//查看指定参数,%是通配符,代表包含百分号之前内容的参数

修改参数

set global max_connect_errors=1000;

注:修改的参数会在重启后恢复,想要永久生效需要在配置文件中进行更改。

查看队列(常用)

show processlist;
show full processlist;//会将最后一行(info)的信息显示完全

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部