Ubuntu-18.04下安裝mysql


安裝mysql服務器

1、 sudo apt-get install mysql-server 

2、 sudo apt-get install mysql-client 

登錄問題

安裝成功后,我們會發現我們沒有登錄的權限。

hhz@hhz-virtual-machine:~$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

 

然后發現當我們輸入 sudo mysql -u root -p 這個命令的時候可以登錄(提示輸入密碼時直接回車即可),顯然這不是我們想要的。

那怎么解決這個問題呢?

解決方案:刪除root用戶,然后重新創建

1、我們先登錄:

hhz@hhz-virtual-machine:~$ sudo mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)

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

 

2、然后查看用戶:

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

 

3、刪除root帳號

mysql> drop user 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)

 

4、重新創建一個root帳號

mysql> create user 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

 

5、接下來我們用 quit; 命令來退出mysql,然后試試用新的帳號登錄

hhz@hhz-virtual-machine:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)

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

 

ok,登錄問題已經完美解決

遠程訪問問題

1、授權

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)

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

 

2、用 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 打開這個文件,然后將 bind-address = 127.0.0.1 改成 bind-address = 0.0.0.0 允許任何IP地址訪問,如果設置多個請用逗號隔開

3、重啟服務: service mysql restart 

4、接下來我們在windows下用Navicat來連接試試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM