sudo apt-get install mysql-server
sudo apt install mysql-client
sudo apt install libmysqlclient-dev
service mysql restart
mysql -uroot -p xxx
当我们在Ubuntu下通过上述方法安装好mysql数据库并进入时,出现这种问题;
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
于是使用sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
把skip-grant-tables添加到[mysqld]里的方法进入数据库
然后设置密码
mysql>update user set authentication_string=password("123456") where user="root";
但这种
会出现这种错误
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '("123456") where user="root"' at line 1
又或者是
mysql> update user set Password="123456";
ERROR 1054 (42S22): Unknown column 'Password' in 'field list'
还有这种不加skip-grant-tables不让你进数据库,加了又不让你改的气死人的死循环局面。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'zlzcam';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
再者就是
mysql> alter user 'root'@'localhost'IDENTIFIED BY 'zlzcam';
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
于是网上查了一大堆,都没能解决问题。
最后通过进入mysql然后输入use mysql查看user表
因为mysql登录验证密码插件是mysql_native_password(mysql.user表中的plugin字段记录的是密码插件)
mysql>select user,plugin from user where user=‘root’;发现
mysql> select user,plugin from user where user='root';
+------+-------------+
| user | plugin |
+------+-------------+
| root | auth_socket |
+------+-------------+
1 row in set (0.00 sec)
然后执行
mysql> update user set plugin=‘mysql_native_password’ where user=‘root’ ;
把auth_socket换成mysql_native_password
再修改密码为123456: alter user’root’@‘localhost’ identified by ‘123456’;
mysql> update user set plugin='mysql_native_password' where user='root' ;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> alter user'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.07 sec)
可以看到修改密码执行成功!
mysql> flush privileges;(这一步必须要有)
exit退出
再用sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
把skip-grant-tables注释掉。
执行service mysql restart;
再mysql -uroot -p
就可以成功输入密码登录到数据库了~
ni@ubuntu:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.20-0ubuntu0.20.04.1 (Ubuntu)