您的位置:首页 > 数据库 > MySQL

阿里云ECS服务器中自带MYSQL无法登陆问题

2017-07-22 21:05 274 查看
阿里云服务器自带mysql数据库,我们可以通过   yum list installed | grep mysql 命令来查看系统装好的mysql。

[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ yum list installed | grep mysql
mysql-community-client.x86_64 5.7.16-1.el7 installed
mysql-community-common.x86_64 5.7.16-1.el7 installed
mysql-community-libs.x86_64 5.7.16-1.el7 installed
mysql-community-server.x86_64 5.7.16-1.el7 installed


启动mysql服务正常:
[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ sudo systemctl start mysqld.service
[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ sudo systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2017-07-22 19:52:38 CST; 23min ago
Process: 4979 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 4962 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 4983 (mysqld)
CGroup: /system.slice/mysqld.service
└─4983 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jul 22 19:52:38 iZ2ze3n5edj5u2cbb9qrijZ systemd[1]: Starting MySQL Server...
Jul 22 19:52:38 iZ2ze3n5edj5u2cbb9qrijZ systemd[1]: Started MySQL Server.


但是登陆mysql时出错:
[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)


我装的时mysql5.7,登陆需要密码才行,在网上找了资料说在 /var/log/mysqld.log 下的日志中会生成初始密码,可我再改文件夹下找不到默认密码,只能通过配置无需验证登录方式登陆进去,然后再重新设置root密码。

1、配置无需验证登录方式

vi /etc/my.cnf

[mysqld]

skip-grant-tables //增加这一行,目的是不用验证就可以登录mysql

2、登陆数据库

命令行输入 mysql,如果不识别,则需要配置环境变量,这里就不多说了

可以先查看下数据库

mysql> show databases; 

mysql> use mysql;

mysql> select * from user;

可以看到user表中的内容

//修改localhost为%

mysql> UPDATE user SETHost = ‘%’ WHEREUser =‘root’;

mysql> flush privileges;

mysql> quit

3、将skip-grant-tables注释掉或者删除

重启服务器

如果还是不行,可能需要修改一下密码

1.先停掉原来的服务

service mysqld stop

 

2.使用安全模式登陆,跳过密码验证

mysqld --user=mysql --skip-grant-tables --skip-networking &

 

3.登陆

mysql -uroot  mysql

 

4.修改密码

mysql  > update user set authentication_string = password(‘密码’) 

       where user = 'root' and Host = ‘%’

 

5.刷新权限

mysql > flush privileges;

 

6.重新使用正常模式登陆

#mysql -uroot -p

#enter password : 密码

 

可以正常登陆。

7.执行其他命令:show databases;

[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ sudo mysqld --user=mysql --skip-grant-tables --skip-networking &
[1] 5190
[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

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

进入mysql数据库,重新设置root登陆密码:

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 |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| 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 |
+---------------------------+
31 rows in set (0.00 sec)


最后重新设置密码:

mysql> update user set authentication_string =password('root') where user = 'root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
[dancheren@iZ2ze3n5edj5u2cbb9qrijZ /]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> ^C
mysql>


至此问题解决了。。。。。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息