一、mysql修改密码:html
root用户时mysql的超级管理员,默认mysql的密码是空的,直接能够链接上去的,不过这样不安全;mysql
注释:为了方便的使用mysql,须要把mysql加入到环境变量里; #后续本身输入mysql能够进入到mysql下;linux
(1):启动mysql后,使用mysql -uroot命令,会提示mysql命令不存在,这是由于安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录不在环境变量PATH里面,因此会报错,若想要这个命令直接运行,须要把PATH作一个更改;web
[root@localhost_001 vhost]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@localhost_001 vhost]# export PATH=$PATH:/usr/local/mysql/bin
注释:export声明的变量临时生效,会在重启后失效;sql
注释:若想变量永久生效,建议PATH=$PATH:/usr/local/mysql/bin写入到/etc/profile文件里,而后执行 source /etc/profile;数据库
(2):进入mysql, 使用mysql -uroot 就能够进入mysql,退出mysql使用quit或者exit便可;vim
[root@localhost_001 vhost]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 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> exit Bye
注释:登陆mysql通常使用 mysql -uroot -p (-p表示指定密码):密码为空时,直接回车也可进入到mysql;安全
(3):设置mysql的登陆密码;(当前mysql没有密码的状况下设置); mysqladminbash
[root@localhost_001 ~]# mysqladmin -uroot password 'nihao123!' Warning: Using a password on the command line interface can be insecure.
注释:提示是警告信息,是说你如今的命令在当前的命令行显示出来了,这样不太安全;socket
登陆mysql;
[root@localhost_001 ~]# mysqladmin -uroot password 'nihao123!' #要求密码登陆; Warning: Using a password on the command line interface can be insecure. [root@localhost_001 ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@localhost_001 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 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>
注释:当设置的密码以后,再次登陆时会提示输入密码才能登陆,如上图;
(4):在知道mysql的密码的状况下,去修改mysql的密码;
[root@localhost_001 ~]# mysqladmin -uroot -p'nihao123!' password 'nihao123@' Warning: Using a password on the command line interface can be insecure.
注释:如上格式: mysqladmin -uroot -p'旧密码' password '新密码'
注释:在明文指定密码的时候,密码能够加单引号,也能够不加单引号,建议加上单引号,防止密码有特殊字符的存在(如果不加单引号,而密码中又有中文字符,则会不识别);
注释:指定新密码登陆,固然也能够不明文登陆, -p回车输入密码也能够;
[root@localhost_001 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 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>
(5):在忘记mysql密码的时候修改mysql的密码;
首先要去mysql的配置文件/etc/my.cnf里加入skip-grant;而后重启mysql服务,再次进入mysql则不须要密码;
skip-grant 表示忽略受权,也就是登陆mysql的时候不须要输入密码,能够直接登陆;
[root@localhost_001 ~]# vim /etc/my.cnf [mysqld] skip-grant datadir=/data/mysql socket=/tmp/mysql.sock [root@localhost_001 ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
(1):登陆mysql,在登陆mysql后,还须要更改一个user表(这个表里存放的用户名和密码),这个表在mysql库里面;(进入到这个库)
[root@localhost_001 ~]# mysql -uroot #进入mysql; Welcome to the MySQL monitor. Commands end with ; or \g. mysql> use mysql; #切换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>
(2):查看user表和password表相关信息;
select * from user; -----> 查看user表,会看到不少的乱码信息,这里保存的mysql的用户名和密码等信息;
select password from user user='root'; 针对root用户查看password表;
mysql> select password from user where user='root'; +-------------------------------------------+ | password | +-------------------------------------------+ | *FA22FC2D09A3E4C4F63AAE8F4AE18DD2F3E7E695 | | | | | | | +-------------------------------------------+ 4 rows in set (0.00 sec)
(3):更改mysql的密码;
update user set password=('111111') where user='root';
密码字段 函数 | 用于加密密码 |为条件语句
mysql> update user set password=password('111111') where user='root'; Query OK, 4 rows affected (0.08 sec) Rows matched: 4 Changed: 4 Warnings: 0
注释:提示说4行则表示密码更改完成;可按以下方式验证;
先去my.cnf的配置文件了注释掉skip-grant(受权登陆);(若是不删除,那么全部的用户都不须要输入密码就能够登陆,这样不安全);
重启mysql服务; service msyqld restart
而后用新密码登陆验证; mysql -uroot -p111111
这样即成功修改可mysql的密码;
链接mysql的方式, 本地链接 or 远程链接;
本地链接:即便没有使用-S来指定socket,默认也是使用/tmp/mysql.sock链接;
mysql -uroot -p11111 <==========> mysql -uroot -p11111 -S/tmp/mysql.sock
[root@localhost_001 ~]# mysql -uroot -p111111 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit Bye [root@localhost_001 ~]# mysql -uroot -p111111 -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. mysql>
注释:socket方式只能使用本地链接使用;
远程链接: mysql -uroot -p密码 -h[远端mysql主机IP地址] -P[远端mysql端口号] #无前后顺序;
[root@localhost_001 ~]# mysql -uroot -p111111 -h127.0.0.1 -P3306 Warning: Using a password on the command line interface can be insecure. mysql> exit Bye
假设咱们从A机器(192.168.149.130)链接B机器(192.168.149.129)的mysql数据库;
[root@localhost_002 ~]# mysql -h192.168.149.129 -P3306 -p111111 Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'192.168.149.130' (using password: YES)
注释:如上链接时提示错误;由于mysql默认是只容许127.0.0.1和localhost登陆,因此须要添加容许192.168.149.130受权登陆;
在B机器(192.168.149.129)执行以下;
mysql> grant all privileges on *.* TO 'root'@'192.168.149.130' identified by 'nihao123!' with grant option; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; #刷新权限; Query OK, 0 rows affected (0.00 sec)
在A机器(192.168.149.130)远程链接便可;
[root@localhost_002 ~]# mysql -h192.168.149.129 -P3306 -pnihao123! Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
注释:在B机器(192.168.149.129)查看受权的用户;
mysql> select host from user where user='root'; +-----------------+ | host | +-----------------+ | 127.0.0.1 | | 192.168.149.130 | | ::1 | | localhost | | localhost\_001 | +-----------------+ 6 rows in set (0.00 sec)
注释:目前的只容许本地和远程主机130登陆,其他主机都拒绝;
注释:通常为了安全性,mysql主机是本地登陆的;
注释:固然若是想让全部用户都链接mysql,能够以下设置;
mysql> grant all privileges on *.* TO 'root'@'%' identified by 'nihao123!' with grant option; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; #刷新权限; Query OK, 0 rows affected (0.00 sec)
1:链接mysql后远程执行命令后退出; mysql -uroot -p111111 -e "show databases;"
[root@localhost_001 ~]# mysql -uroot -p111111 -e "show databases;" Warning: Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
远程链接执行命令; mysql -h192.168.149.129 -uroot -pnihao123! -P3306 -e “show databases;”
[root@localhost_002 ~]# mysql -h192.168.149.129 -uroot -pnihao123! -P3306 -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; 切换库 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;
1:建立库; create databases db1;
mysql> create database db1; Query OK, 1 row affected (0.00 sec)
2:查看有哪些库; show databases;
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
3:切换库; use mysql;
mysql> use db1; Database changed
4:建立表; create tables 后面加字段;
mysql> create table t1(`id` int(4), `name` char(40)); Query OK, 0 rows affected (0.03 sec)
5:查看表里的字段; show create table t1\G; desc user; -------> 查看user表有哪些字段;
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) ERROR: No query specified mysql> desc t1\G *************************** 1. row *************************** Field: id Type: int(4) Null: YES Key: Default: NULL Extra: *************************** 2. row *************************** Field: name Type: char(40) Null: YES Key: Default: NULL Extra: 2 rows in set (0.00 sec)
注释:在命令前面加#号则表示取消这个命令;
删除一个表; drop table t1;
mysql> drop table t1; Query OK, 0 rows affected (0.03 sec)
从新建立一个表,并指定字符集为utf-8;
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.02 sec)
再次查看会发现字符集变成了utf-8;
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
6:查看当当前所在库; select database();
mysql> select database(); +------------+ | database() | +------------+ | db1 | +------------+ 1 row in set (0.00 sec)
7:查看当前登陆数据库的用户; select user();
mysql> select user(); +--------------------+ | user() | +--------------------+ | root@localhost_001 | +--------------------+ 1 row in set (0.00 sec)
8:查看当前数据库的版本; select version();
mysql> select version(); +-----------+ | version() | +-----------+ | 5.6.39 | +-----------+ 1 row in set (0.00 sec)
show status; 列出mysql的数据库状态,会把经常使用数据都列出来;
9:查看mysql的各个参数(在/etc/my.cnf定义的参数);
查看mysql定义各个参数: show variables;
查看指定的参数,用like; show variables like 'max_connect%';
mysql> show variables like 'max_connect%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 100 | | max_connections | 151 | +--------------------+-------+ 2 rows in set (0.00 sec)
10:修改参数,不过仅在内存中生效; set global max_connect_errors=1000;
mysql> show variables like 'max_connect%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 1000 | | max_connections | 151 | +--------------------+-------+ 2 rows in set (0.00 sec)
注释:如果想从新后生效,能够修改/etc/my.cnf配置文件;
11:查看队列; show processlist; 用来查看有哪些用户在连mysql,在作什么操做,是否有锁表;
mysql> show processlist; +----+------+---------------------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+------+---------+------+-------+------------------+ | 6 | root | localhost_001:55484 | db1 | Query | 0 | init | show processlist | +----+------+---------------------+------+---------+------+-------+------------------+ 1 row in set (0.00 sec)
12: show full processlist; #同查看队列命令,不会最后一列会较详细的显示出来;
mysql> show full processlist; +----+------+---------------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+------+---------+------+-------+-----------------------+ | 6 | root | localhost_001:55484 | db1 | Query | 0 | init | show full processlist | +----+------+---------------------+------+---------+------+-------+-----------------------+ 1 row in set (0.00 sec)
注释:
mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/ 知乎上的答案 https://www.zhihu.com/question/20596402 mysql 配置详解:https://www.jb51.net/article/48082.htm mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html 同窗分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html