Ubuntu安裝MySQL
1.查看系統有沒有安裝MySQL
$ dpkg -l | grep mysql
2.下載MySQL
$ apt install mysql-server
3.查看MySQl運行狀態
$ netstat -nltp
$ ps -ef | grep mysql
4.登錄
此時登錄提示: ERROR 1045 (28000): Access denied for user ‘root’@'localhost’
解決辦法:
1)查看/etc/mysql/debian.cnf
,即可看到密碼
$ sudo cat debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = ApOiIr4jQa9lEXRC
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = ApOiIr4jQa9lEXRC
socket = /var/run/mysqld/mysqld.sock
3)關閉valid_password
修改/etc/mysql/mysql.conf.d/mysqld.cnf,添加如下內容
validate_password.check_user_name = OFF
validate_password.dictionary_file =
validate_password.length = 6
validate_password.mixed_case_count = 0
validate_password.number_count = 0
validate_password.policy = LOW
重啓服務
$ systemctl restart mysql
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: user
Password:
==== AUTHENTICATION COMPLETE ===
2)登錄數據庫,並修改密碼
$ mysql -u debian-sys-maint -pApOiIr4jQa9lEXRC
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.17-0ubuntu2 (Ubuntu)
Copyright (c) 2000, 2019, 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.
mysql>
mysql>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> Query OK, 0 rows affected (0.00 sec)
3)使用root登錄
$ mysql -u root -p123456
5.添加遠程訪問權限
1)修改root的host,把需要遠程訪問的用戶的host改成%
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | localhost |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
mysql> update user set host='%' where user='root';
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
2)修改監聽IP地址
已和mysql的監聽地址是127.0.0.1,只能本地訪問
修改/etc/mysql/mysql.conf.d/mysqld.cnf,修改綁定地址爲0.0.0.0
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
3)重啓服務
$ systemctl restart mysql.service