Ubuntu 安装 mysql && Ubuntu 安装 mysql 忘记初始密码解决方法

查看是否安装Mysql

sudo netstat -tap | grep mysql

如果为空则没有安装,进行安装:

sudo apt-get install mysql-server mysql-client

根据提示输入密码,然后确认..

Mysql 连接测试

mysql -u root -h localhost -p

启动,停止,重启 Mysql命令

sudo service mysql start
sudo service mysql stop
sudo service mysql restart

Ubuntu 安装 mysql 忘记初始密码解决方法

//打开这个文件 /etc/mysql/debian.cnf
//查看默认分配的密码
[client]
host = localhost
user = debian-sys-maint
password = eyPDN7kavhmjCZUn (记住这个密码)
socket = /var/run/mysqld/mysqld.sock

输入命令进入mysql修改用户密码

// 输入命令后把上面的密码粘贴进去
mysql -udebian-sys-maint -p
//进入到mysql界面厚修改密码
update mysql.user set authentication_string=password('newpassword') where user='root'

//都要使用刷新权限列表
flush privileges;

重新启动Mysql服务即可

如果出现下列错误请参考解决方式
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’

https://stackoverflow.com/questions/39281594/error-1698-28000-access-denied-for-user-rootlocalhost

============================================================================================

============================================================================================

============================================================================================

The reason is that recent Ubuntu installation (maybe others also), mysql is using by default the UNIX auth_socket plugin.

Basically means that: db_users using it, will be "auth" by the system user credentias. You can see if your root user is set up like this by doing the following:

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;

+------------------+-----------------------+
| User             | plugin                |
+------------------+-----------------------+
| root             | auth_socket           |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+

As you can see in the query, the root user is using the auth_socket plugin

There are 2 ways to solve this:

  1. You can set the root user to use the mysql_native_password plugin
  2. You can create a new db_user with you system_user (recommended)

Option 1:

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

$ service mysql restart

Option 2: (replace YOUR_SYSTEM_USER with the username you have)

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
mysql> FLUSH PRIVILEGES;
mysql> exit;

$ service mysql restart

 

=======================================================

=======================================================

=======================================================

可以通过如下命令进入MySQL服务:

mysql -uroot -p你的密码

现在设置mysql允许远程访问,首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf:

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

注释掉bind-address = 127.0.0.1:

保存退出,然后进入mysql服务,执行授权命令:

grant all on *.* to root@'%' identified by '你的密码' with grant option;

flush privileges;

然后执行quit命令退出mysql服务,执行如下命令重启mysql:

service mysql restart

 

转载于:https://my.oschina.net/u/188924/blog/1923866

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值