Ubuntu 安裝 mysql && Ubuntu 安裝 mysql 忘記初始密碼解決方法

查看是否安裝Mysql

sudo netstat -tap | grep mysql

若是爲空則沒有安裝,進行安裝:html

sudo apt-get install mysql-server mysql-client

根據提示輸入密碼,而後確認..mysql

Mysql 鏈接測試

mysql -u root -h localhost -p

啓動,中止,重啓 Mysql命令sql

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服務便可socket

若是出現下列錯誤請參考解決方式
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’ide

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

============================================================================================ui

============================================================================================this

============================================================================================spa

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

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

相關文章
相關標籤/搜索