查看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.
主要用到的是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';