Linux下添加MySql组件后报无权限问题解决
Tomcat日志报错如下:
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344) at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38) at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294) at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247) at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221) ... 85 more
使用mysql命令行连接,依旧报错:
root@NaiYou:~# mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) root@NaiYou:~# mysql -u root -p ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
尝试了N种方法,最终用以下方法解决了:
1、首先到安装目录下找到初始化的账号密码 root@NaiYou:~# cd /etc/mysql/ root@NaiYou:/etc/mysql# ls conf.d debian.cnf debian-start my.cnf 2、查看账户密码 root@NaiYou:/etc/mysql# cat debian.cnf # Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = XXXXXXXX socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = XXXXXXXX socket = /var/run/mysqld/mysqld.sock basedir = /usr 3、使用client的账户密码登陆mysql命令行 root@NaiYou:/etc/mysql# mysql -udebian-sys-maint -p 4、更新root用户的密码 mysql> use mysql; mysql> UPDATE user SET Password=PASSWORD('123456') where USER='root'; mysql> quit 5、现在可以用Root用户登陆了root@NaiYou:/etc/mysql# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2247 Server version: 5.5.44-0ubuntu0.14.04.1 (Ubuntu)