lihanchuan125 发表于 2018-10-6 06:49:17

13.1-13.3 设置更改root密码,连接MySQL,MySQL常用命令

  13.1 设置更改root密码
  大纲

  准备工作:
  1 启动mysql服务
  # /etc/init.d/mysqld start
  Starting MySQL.                                          
  2 设置MySQL环境变量
  因为#mysql命令需要使用绝对路径/ /usr/local/mysql/bin/mysql,这样太麻烦了,
  所以需要更改环境变量PATH,增加mysql绝对路径。
  # ls /usr/local/mysql/bin/mysql
  /usr/local/mysql/bin/mysql
  2.1 添加环境变量
  # export PATH=$PATH:/usr/local/mysql/bin/
  测试使用mysql命令

  2.2 使命令用久生效 把命令放在profile的最后面
  # vim /etc/profile
  unset -f pathmunge
  export PATH=$PATH:/usr/local/mysql/bin/
  ~
  ~
  source /etc/profile 使其用久生效。
  # source /etc/profile
  3 进入mysql
  -u指定用户,-p指定密码(如果无密码,直接回车进入)
  # mysql -uroot -p
  mysql> quit
  Bye
  4 因为MySQL第一次进去是不需要密码的,比较危险,所以需要设置密码
  # mysqladmin -uroot password 'aminglinux.1'
  Warning: Using a password on the command line interface can be insecure.
  测试指定密码进入
  # mysql -uroot -p
  4.1 更改密码
  # mysqladmin -uroot -p'aminglinux.1' password 'aminglinux.2'
  Warning: Using a password on the command line interface can be insecure.
  5 密码重置(在没有root密码的时候修改root密码)
  在增加skip-grant,此配置的意义就是忽略授权,加入配置后,不需要登录密码即可进入MySQL
  #vi /etc/my.cnf
  # vim /etc/my.cnf
  
  skip-grant
  datadir=/data/mysql
  socket=/tmp/mysql.sock
  5.1 配置完成后,重启服务使其生效。
  # /etc/init.d/mysqld restart
  Shutting down MySQL..                                    
  Starting MySQL.                                          
  5.2 测试
  # mysql -uroot
  直接进入,无需任何认证。
  mysql>
  5.3 使用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
  5.4 进入密码表
  mysql> select password from user ;
  +-------------------------------------------+
  | password                                  |
  +-------------------------------------------+
  | *C2586DB1E5698A5F1DC57808497DA087CC1EF767 |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  +-------------------------------------------+
  6 rows in set (0.01 sec)
  5.5 在密码表输入命令,更改密码
  mysql> update user set password=password('aminglinux') where user='root';
  Query OK, 4 rows affected (0.00 sec)
  Rows matched: 4Changed: 4Warnings: 0
  密码更新成功,quit退出mysql
  mysql> quit
  Bye
  
  6 修改完成后,需要把跳过验证参数(skip-grant)删除,不然会发生很大安全隐患。
  # vim /etc/my.cnf
  
  datadir=/data/mysql
  socket=/tmp/mysql.sock
  修改后,重启服务
  # /etc/init.d/mysqld restart
  Shutting down MySQL..                                    
  Starting MySQL.                                          
  6.1 用新密码测试登录MySQL
  # mysql -uroot -paminglinux
  Warning: Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.Commands end with ; or \g.
  13.2 连接MySQL
  1 本地 ip:port连接
  # mysql -uroot -paminglinux -h127.0.0.1 -P3306

  2 还可以利用socket去连接。MySQL的socket在/tmp/mysql.sock
  检查mysql.sock的监听状态
  # !ps
  ps aux |grep mysql.sock

  socket连接
  # mysql -uroot -paminglinux -S/tmp/mysql.sock
  这种情况只适合用在本机上(这种方法和第一种是一样的,也是默认的连接方法)
  3 连接MySQL顺便操作一些命令
  # mysql -uroot -paminglinux -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命令。
  这种情况适用在shell脚本,例如监听MySQL的连接数。
  13.3 MySQL常用命令
  慎记,MySQL里的命令在Linux上是执行不到的。但是通过mysql -e "命令" 可以指定执行。
  在MySQL里面执行命令,以 ; 结束。
  连接MySQL
  # mysql -uroot -paminglinux
  查询库 show databases;
  切换库 use mysql;
  查看库里的表 show tables;
  查看整张表的内容 select * from tb_name;
  查看表里的字段 desc tb_name;
  字段,表,库三者的关系,库是由表组成,表是有字段组成。
  操作:
  查看user字段,
  1 先切换库
  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
  2 再查看user字段,
  mysql> desc user;

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

  查看当前用户 select user();
  mysql> select user();
  +----------------+
  | user()         |
  +----------------+
  | root@localhost |
  +----------------+
  1 row in set (0.00 sec)
  当前用户是localhost,其实127.0.0.1连接进来也是localhost,因为127.0.0.1就是localhost.
  接下来试试用本机ip连接看看
  # mysql -h172.18.171.157 -paminglinux
  mysql> select user();
  +-------------+
  | user()      |
  +-------------+
  | root@AliKvn |
  +-------------+
  1 row in set (0.00 sec)
  AliKvn是本机的主机名,而172.18.171.157被传解析了,所以AliKvn是被172.18.171.157解析出来的。
  MySQL的命令历史记录存在/root/.mysql_history
  查看方法 less .mysql_history
  查看当前使用的数据库 select databsase();
  mysql> select database();
  +------------+
  | database() |
  +------------+
  | NULL       |
  +------------+
  1 row in set (0.00 sec)
  NULL为空,例如切换到mysql库下,这里不会显示空。
  mysql> use mysql
  Database changed
  mysql>select database();
  +------------+
  | database() |
  +------------+
  | mysql      |
  +------------+
  1 row in set (0.00 sec)
  创建库 create database db1;
  mysql> create database db1;
  Query OK, 1 row affected (0.00 sec)
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | db1                |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  5 rows in set (0.00 sec)
  创建表 create table t1(`id` int(4), `name` char(40));
  1 先切换db1库
  mysql> use db1;
  Database changed
  2 再创建表t1
  mysql> create table t1(`id` int(4), `name` char(40));
  Query OK, 0 rows affected (0.02 sec)
  3 查看创建的表
  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)
  定义utf8格式
  操作:
  1 删除表
  drop table t1;
  mysql> drop table t1;
  Query OK, 0 rows affected (0.01 sec)
  2 定义utf8格式
  mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  Query OK, 0 rows affected (0.02 sec)
  查看刚刚创建t1的表(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;
  mysql> show variables;

  查看指定参数,%表示通配符
  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;(如果想永久生效,需要修改/etc/my.conf,添加相关配置参数)
  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)
  max_connect_errors 1000数值变成了1000
  查看队列 两者体验在info上,full的比较完整。(查看队列命令用得最多,需要熟练使用)
  mysql> show processlist;
  +----+------+--------------+------+---------+------+-------+------------------+

  |>  +----+------+--------------+------+---------+------+-------+------------------+
  | 13 | root | AliKvn:56928 | NULL | Query   |    0 | init| show processlist |
  +----+------+--------------+------+---------+------+-------+------------------+
  1 row in set (0.00 sec)
  这个比较完整查看队列。
  show full processlist;
  mysql> show full processlist;
  +----+------+--------------+------+---------+------+-------+-----------------------+

  |>  +----+------+--------------+------+---------+------+-------+-----------------------+
  | 13 | root | AliKvn:56928 | NULL | Query   |    0 | init| show full processlist |
  +----+------+--------------+------+---------+------+-------+-----------------------+
  1 row in set (0.00 sec)

页: [1]
查看完整版本: 13.1-13.3 设置更改root密码,连接MySQL,MySQL常用命令