MySQL密碼更改,經常使用命令

MySQL密碼更改

查看MySQL服務是否啓動,若是沒有啓動使用/etc/init.d/mysqld start啓動mysql

[root@test-a ~]# ps aux | grep mysql
root      2180  0.0  0.1 115432  1724 ?        S    07:33   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/usr/local/mysql/mysqld.pid
mysql     2390  0.9 17.8 1117648 180356 ?      Sl   07:33   0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/db.err --pid-file=/usr/local/mysql/mysqld.pid --socket=/usr/local/mysql/mysql.sock --port=3306
root      2490  0.0  0.0 112704   972 pts/0    R+   07:35   0:00 grep --color=auto mysql

登陸MySQL,改密碼sql

[root@test-a ~]# mysql # 沒有找到該命令,由於沒有爲MySQL設置環境變量
-bash: mysql: command not found
[root@test-a ~]# export PATH=$PATH:/usr/local/mysql/bin/  
[root@test-a ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23

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.
[root@test-a ~]# mysqladmin -uroot -p'test111'  password test222 #更改密碼
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
# 出現這個錯誤,網上基本都是說沒有把對應的mysql.sock放到/tmp目錄下,可是個人配置裏是/usr/local/mysql/mysql.sock
[root@test-a ~]# cat /etc/my.cnf
[mysql]
socket          = /usr/local/mysql/mysql.sock
# The MySQL server
[mysqld]
port            = 3306
socket          = /usr/local/mysql/mysql.sock
#skip-grant-tables
datadir=/data/mysql
log-error=/usr/local/mysql/db.err
pid-file=/usr/local/mysql/mysqld.pid
character-set-server = utf8
# 以爲多是配置文件的問題,就去找差別,發現本身沒有client配置,從新配置,解決問題  
[root@test-a ~]# vim /etc/my.cnf
[root@test-a ~]# cat /etc/my.cnf
[client]
socket          = /usr/local/mysql/mysql.sock
# The MySQL server
[mysqld]
port            = 3306
socket          = /usr/local/mysql/mysql.sock
#skip-grant-tables
datadir=/data/mysql
log-error=/usr/local/mysql/db.err
pid-file=/usr/local/mysql/mysqld.pid
character-set-server = utf8
[mysql.server]
basedir=/usr/local/mysql

[root@test-a ~]# mysqladmin -uroot -p'test111'  password test222
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

MySQL忘記root用戶的密碼重置

主要用到的是skip-grant-tables,在配置文件中配置該字段,重啓數據庫

[root@test-a ~]# vim /etc/my.cnf
[root@test-a ~]# cat /etc/my.cnf
[client]
socket          = /usr/local/mysql/mysql.sock
# The MySQL server
[mysqld]
port            = 3306
socket          = /usr/local/mysql/mysql.sock
skip-grant-tables
datadir=/data/mysql
log-error=/usr/local/mysql/db.err
pid-file=/usr/local/mysql/mysqld.pid
character-set-server = utf8
[mysql.server]
basedir=/usr/local/mysql
[root@test-a ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@test-a ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 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> update user set authentication_string=password('test111') where user='root'; # MySQL5.7以後是authentication_string,若是以前則是password字段
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

# 或者直接使用下面的語句
mysql> alter user 'root'@'localhost' identified by 'test111';

MySQL鏈接方式

  • mysql -uroot -p'test222' # 直接輸入密碼鏈接
  • mysql -uroot -p'test111' -h127.0.0.1 -P3306 # 遠程鏈接方式
  • mysql -uroot -p'test111' -S/usr/local/mysql/mysql.sock # socket鏈接, 只支持本地鏈接
  • mysql -uroot -p'test111' -e "show databases" # 鏈接以後經過-e執行命令

MySQL經常使用命令

  • 列出全部庫: show databases;
  • 切換庫: use mysql;
  • 列出庫裏的表: show tables;
  • 查看錶裏的字段: desc user;
  • 查看錶建立的語句: show create table user\G # \G將查到的結果縱向顯示
  • 查看當前用戶: select user(); # 結果是"用戶名@主機名"
  • 查看當前使用的數據庫: select database();
  • 建立庫: create database test_db0;
  • 建立表: create table test_tb0(`id` int(4));
  • 刪除表: drop table test_tb0;
  • 查看數據庫版本: select version();
  • 查看數據庫狀態: show status;
  • 查看參數: show variables; 能夠模糊匹配 show variables like 'max_connect%'; #這些參數均可以在my.cnf中定義
  • 修改參數 set global max_connect_errors=1000; # 更改只在內存中生效
  • 查看隊列 show processlist; show full processlist;
相關文章
相關標籤/搜索