mysql连接的密码_1.MySQL密码相关,连接方式,常用命令

[toc]

MySQL密码相关,连接方式,常用命令

一、MySQL密码修改

1. 判断mysql是否开启

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

root 2544 0.0 0.0 112680 972 pts/0 S+ 10:02 0:00 grep --color=auto mysql

[root@xavi ~]# /etc/init.d/mysqld start

Starting MySQL.... SUCCESS!

[root@xavi ~]# !ps

ps aux |grep mysql

root 2738 0.0 0.0 113268 1584 pts/0 S 10:12 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/xavi.pid

mysql 2886 1.9 24.1 975152 451864 pts/0 Sl 10:12 0:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/xavi.err --pid-file=/data/mysql/xavi.pid --socket=/tmp/mysql.sock

root 2943 0.0 0.0 112680 976 pts/0 S+ 10:15 0:00 grep --color=auto mysql

1.1 启动sql命令,但是无法启动,原因是mysql的命令路径并未在环境变量$PATH内定义过

[root@xavi ~]# mysql -uroot

bash: mysql: 未找到命令...

[root@xavi ~]# ls /usr/local/mysql/bin/mysql

/usr/local/mysql/bin/mysql

[root@xavi ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@xavi ~]# export PATH=$PATH:/usr/local/mysql/bin/

[root@xavi ~]# 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> Ctrl-C -- exit!

Aborted

1.2 如果需要将mysql路径永久添加到环境变量中,需要进入/etc/profile进行编辑,同时source /etc/profile加载配置

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

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

AG74aAjm2h.png?imageslim

2.给MySQL建立密码:mysqladmin -uroot password 'xavilinux.1',然后登入

[root@xavi ~]# mysqladmin -uroot password 'xavilinux.1'

Warning: Using a password on the command line interface can be insecure.

[root@xavi ~]# mysql -uroot

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

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

Enter password:

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

Your MySQL connection id is 7

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>

2.1修改原密码

[root@xavi ~]# mysqladmin -uroot -p'xavilinux.1' password 'xavilinux'

Warning: Using a password on the command line interface can be insecure.

2.2登入,记得密码的单引号,和-p后面没有空格的

[root@xavi ~]# mysql -uroot -p'xavilinux'

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 10

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.

2.3 在忘记密码的情况下如何进入

a.先进入vim /etc/my.cnf,增加skip-grant,忽略授权

fmK5a10ble.png?imageslim

b.保存后重启MySQL服务,进入

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

Shutting down MySQL.... SUCCESS!

Starting MySQL. SUCCESS!

[root@xavi ~]# 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>

c.查找密码所在的数据表

use mysql ;

FcbijBL2jg.png?imageslim

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> select password from user ;

+-------------------------------------------+

| password |

+-------------------------------------------+

| *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |

| |

| |

| |

| |

| |

+-------------------------------------------+

6 rows in set (0.00 sec)

d.修改密码:update user set password=password('xavilinux') where user='root' ;

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

Query OK, 3 rows affected (0.00 sec)

Rows matched: 4 Changed: 3 Warnings: 0

e.面密码修改成功后,修改下/etc/my.cnf文件中,前面添加的skip-grant删除,恢复密码验证,确保安全

E8Kf3gA7fB.png?imageslim

f.重启服务,输入密码登入sql

[root@xavi ~]# vi /etc/my.cnf

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

ERROR! MySQL server PID file could not be found!

Starting MySQL... SUCCESS!

[root@xavi ~]# mysql -uroot -pxavilinux

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 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>

二、连接数据库的方式

1.mysql -uroot -pxavilinux

[root@xavi ~]# mysql -uroot -pxavilinux

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 1

Server version: 5.6.35 MySQL Community Server (GPL)

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

2.mysql -uroot -pxavilinux -h127.0.0.1 -P3306

[root@xavi ~]# mysql -uroot -pxavilinux -h127.0.0.1 -P3306

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 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.

3.用socke方式连接mysql -uroot -pxavilinux -S/tmp/mysql.sock,只适合本机登入

Ce0e36Hhja.png?imageslim

[root@xavi ~]# mysql -uroot -pxavilinux -S/tmp/mysql.sock

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.

mysql -uroot -pxavilinux -e "show databases"//shell脚本里面常用

[root@xavi ~]# mysql -uroot -pxavilinux -e "show databases"

Warning: Using a password on the command line interface can be insecure.

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

三、MySQL基本操作的常用命令

记住MySQL的操作必须进入mysql后执行

[root@xavi ~]# mysql -uroot -pxavilinux

3.1 查询当前库

mysql> show databases; //命令结尾加上分号

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

4 rows in set (0.00 sec)

3.2 切换到某个数据库

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

3.3 查看表

mysql> show tables;

+---------------------------+

| Tables_in_mysql |

+---------------------------+

| columns_priv |

| db |

| event |

| func |

| general_log |

| help_category |

| help_keyword |

| help_relation |

| help_topic |

| innodb_index_stats |

| innodb_table_stats |

| ndb_binlog_index |

| plugin |

| proc |

| procs_priv |

| proxies_priv |

| servers |

| slave_master_info |

| slave_relay_log_info |

| slave_worker_info |

| slow_log |

| tables_priv |

| time_zone |

| time_zone_leap_second |

| time_zone_name |

| time_zone_transition |

| time_zone_transition_type |

| user |

+---------------------------+

28 rows in set (0.00 sec)

3.4 查看某个表的全部字段

mysql> desc db;

+-----------------------+---------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-----------------------+---------------+------+-----+---------+-------+

| Host | char(60) | NO | PRI | | |

| Db | char(64) | NO | PRI | | |

| User | char(16) | NO | PRI | | |

| Select_priv | enum('N','Y') | NO | | N | |

| Insert_priv | enum('N','Y') | NO | | N | |

| Update_priv | enum('N','Y') | NO | | N | |

| Delete_priv | enum('N','Y') | NO | | N | |

| Create_priv | enum('N','Y') | NO | | N | |

| Drop_priv | enum('N','Y') | NO | | N | |

| Grant_priv | enum('N','Y') | NO | | N | |

| References_priv | enum('N','Y') | NO | | N | |

| Index_priv | enum('N','Y') | NO | | N | |

| Alter_priv | enum('N','Y') | NO | | N | |

| Create_tmp_table_priv | enum('N','Y') | NO | | N | |

| Lock_tables_priv | enum('N','Y') | NO | | N | |

| Create_view_priv | enum('N','Y') | NO | | N | |

| Show_view_priv | enum('N','Y') | NO | | N | |

| Create_routine_priv | enum('N','Y') | NO | | N | |

| Alter_routine_priv | enum('N','Y') | NO | | N | |

| Execute_priv | enum('N','Y') | NO | | N | |

| Event_priv | enum('N','Y') | NO | | N | |

| Trigger_priv | enum('N','Y') | NO | | N | |

+-----------------------+---------------+------+-----+---------+-------+

22 rows in set (0.01 sec)

Filed:字段名,type:字段格式

3.5 查看建表语句并列出,命令后加\G,目的是让列出来的结果竖排显示,这样看起来更清晰;

mysql> show create table user\G;

*************************** 1. row ***************************

Table: user

Create Table: CREATE TABLE `user` (

`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',

`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',

`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',

`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',

`ssl_cipher` blob NOT NULL,

`x509_issuer` blob NOT NULL,

`x509_subject` blob NOT NULL,

`max_questions` int(11) unsigned NOT NULL DEFAULT '0',

`max_updates` int(11) unsigned NOT NULL DEFAULT '0',

`max_connections` int(11) unsigned NOT NULL DEFAULT '0',

`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',

`plugin` char(64) COLLATE utf8_bin DEFAULT 'mysql_native_password',

`authentication_string` text COLLATE utf8_bin,

`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

PRIMARY KEY (`Host`,`User`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'

1 row in set (0.01 sec)

*不加\G

D5K52JHf6k.png?imageslim

3.6 查看当前是哪个用户

mysql> select user();

+----------------+

| user() |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.00 sec)

3.7 查看mysql历史指令记录情况

ls -la

74F2l86hjK.png?imageslim

[root@xavi ~]# less .mysql_history

_HiStOrY_V2_

user\040mysql\040;

use\040mysql\040;

select\040*\040from\040user

select\040*\040from\040user\040;

select\040*\040from\040user\040select\040*\040from\040user;

use\040mysql

show\040databeses\040;

show\040databases;

use\040mysql;

show\040tables;

desc\040db;

shwo\040create\040teble\040user\134G;

show\040create\040teble\040user\134G;

show\040create\040table\040user\134G;

show\040create\040table\040user;

select\040user();

3.8 查看当前所使用的数据库

切换回来

[root@xavi ~]# !mys

mysql -uroot -pxavilinux

mysql> select database();

+------------+

| database() |

+------------+

| NULL |

+------------+

1 row in set (0.01 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> select database();

+------------+

| database() |

+------------+

| mysql |

+------------+

1 row in set (0.00 sec)

3.9 创建一个新库

mysql> create database db1;

Query OK, 1 row affected (0.01 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| db1 |

| mysql |

| performance_schema |

| test |

+--------------------+

5 rows in set (0.03 sec)

3.10 创建一个新表

mysql> use db1;

Database changed

mysql> create table t1(`id` int(4), `name` char(40));

Query OK, 0 rows affected (0.03 sec)

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)

3.11删除一个表,并指定表的ENGINE和CHARSET

mysql> drop table t1;

Query OK, 0 rows affected (0.01 sec)

mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.04 sec)

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)

3.12 查看数据库的版本

mysql> select version();

+-----------+

| version() |

+-----------+

| 5.6.35 |

+-----------+

1 row in set (0.00 sec)

3.13 查看MySQL的当前状态

mysql> show status;

+-----------------------------------------------+-------------+

| Variable_name | Value |

+-----------------------------------------------+-------------+

| Aborted_clients | 0 |

| Aborted_connects | 0 |

| Binlog_cache_disk_use | 0 |

| Binlog_cache_use | 0 |

| Binlog_stmt_cache_disk_use | 0 |

| Binlog_stmt_cache_use | 0 |

| Bytes_received | 1248 |

| Bytes_sent | 22408 |

| Com_admin_commands | 0 |

| Com_assign_to_keycache | 0 |

3.14 查看MySQL的参数,其中很多参数都是可以在/etc/my.cnf中定义,部分参数可在线编辑

mysql> show variables;

3.15 模糊查找某参数

mysql> show variables like 'max_connect%';

+--------------------+-------+

| Variable_name | Value |

+--------------------+-------+

| max_connect_errors | 100 |

| max_connections | 151 |

+--------------------+-------+

2 rows in set (0.00 sec)

mysql> show variables like 'slow%';

+---------------------+---------------------------+

| Variable_name | Value |

+---------------------+---------------------------+

| slow_launch_time | 2 |

| slow_query_log | OFF |

| slow_query_log_file | /data/mysql/xavi-slow.log |

+---------------------+---------------------------+

3 rows in set (0.00 sec)

3.16 修改某参数

mysql> set global max_connect_errors=1000;

Query OK, 0 rows affected (0.02 sec)

mysql> show variables like 'max_connect_errors';

+--------------------+-------+

| Variable_name | Value |

+--------------------+-------+

| max_connect_errors | 1000 |

+--------------------+-------+

1 row in set (0.01 sec)

3.17 查看当前MySQL服务器队列,查看MySQL在干什么,是否有锁表

mysql> show processlist;

+----+------+-----------+------+---------+------+-------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+------+-----------+------+---------+------+-------+------------------+

| 6 | root | localhost | db1 | Query | 0 | init | show processlist |

+----+------+-----------+------+---------+------+-------+------------------+

1 row in set (0.00 sec)

mysql> show full processlist;

+----+------+-----------+------+---------+------+-------+-----------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+------+-----------+------+---------+------+-------+-----------------------+

| 6 | root | localhost | db1 | Query | 0 | init | show full processlist |

+----+------+-----------+------+---------+------+-------+-----------------------+

1 row in set (0.00 sec)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值