1. 程式人生 > >2018-05-07 Linux學習

2018-05-07 Linux學習

Linux學習

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

操作過程

添加PATH
[root@linux-01 ~]# mysql -uroot

-bash: mysql: 未找到命令
[root@linux-01 ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql

[root@linux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@linux-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@linux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin/

[root@linux-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

設置密碼

[root@linux-01 ~]# mysqladmin -uroot password ‘aminglinux.1‘
Warning: Using a password on the command line interface can be insecure.
[root@linux-01 ~]# mysql -uroot -p
Enter password: 
輸入密碼能正常登陸

更改密碼

[root@linux-01 ~]# mysqladmin -uroot -p‘aminglinux.1‘ password ‘aminglinux.2‘
Warning: Using a password on the command line interface can be insecure.

[root@linux-01 ~]# mysql -uroot -p
Enter password: 
輸入密碼能正常登陸

重置密碼 (不知密碼)

[root@linux-01 ~]# vim /etc/my.cnf
[mysqld]
skip-grant
datadir=/data/mysql
socket=/tmp/mysql.sock

[root@linux-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@linux-01 ~]# mysql -uroot

mysql> use mysql

mysql> select * from user;

mysql> update user set password=password(‘aminglinux‘) where user=‘root‘;

[root@linux-01 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

[root@linux-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

[root@linux-01 ~]# mysql -uroot -p
Enter password: 
輸入密碼能正常登陸

13.2 連接MySQL

mysql -uroot -p123456
mysql -uroot -p123456 -h127.0.0.1 -P3306
mysql -uroot -p123456 -S/tmp/mysql.sock
mysql -uroot -p123456 -e “show databases”

[root@linux-01 ~]# 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               |
+--------------------+

13.3 MySQL常用命令

查詢庫 show databases;
切換庫 use mysql;
查看庫裏的表 show tables;
查看表裏的字段 desc tb_name;
查看建表語句 show create table tb_name\G;
查看當前用戶 select user();
查看當前使用的數據庫 select database();

創建庫 create database db1;
創建表 use db1; create table t1(id int(4), name char(40));
查看當前數據庫版本 select version();
查看數據庫狀態 show status;
查看各參數 show variables; show variables like ‘max_connect%‘;
修改參數 set global max_connect_errors=1000;
查看隊列 show processlist; show full processlist;

操作過程

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

mysql> desc user;

mysql> show create table user\G;

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

會反解析登陸ip

[root@linux-01 ~]# mysql -uroot -paminglinux -h192.168.106.160

mysql> select user();
+---------------+
| user()        |
+---------------+
| root@linux-01 |
+---------------+
1 row in set (0.00 sec)

查看庫

mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row 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> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

創建庫

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> use db1;
Database changed

創建表

mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.01 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.01 sec)

ERROR: 
No query specified

創建表,指定字符串

mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;                                                                        Query OK, 0 rows affected (0.01 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)

ERROR: 
No query specified

查看版本

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

查看數據庫狀態

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

查看各參數

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)

mysql> show variables like ‘slow%‘;
+---------------------+-------------------------------+
| Variable_name       | Value                         |
+---------------------+-------------------------------+
| slow_launch_time    | 2                             |
| slow_query_log      | OFF                           |
| slow_query_log_file | /data/mysql/linux-01-slow.log |
+---------------------+-------------------------------+
3 rows in set (0.00 sec)

修改參數,臨時生效,若長期生效需要改配置文件 my.conf

mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.01 sec)

查看隊列

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 10 | 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                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 10 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

2018-05-07 Linux學習