直接使用:
1 |
sudo apt-get install mysql-server |
,默认安装的是5.7,过程中未提示设置密码,使用空密码登陆报错:
1 2 3 |
hsm@ubuntu:~$ mysql -uroot -p Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost' |
初始密码保存在:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
hsm@ubuntu:~$ sudo cat /etc/mysql/debian.cnf [sudo] password for hsm: # Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = cflsRqU65pi5S3wo socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = cflsRqU65pi5S3wo socket = /var/run/mysqld/mysqld.sock |
使用自动生成的用户登陆:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
hsm@ubuntu:~$ mysql -udebian-sys-maint -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.33-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> |
然后设置root密码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set authentication_string=PASSWORD("root") where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> update user set plugin="mysql_native_password"; Query OK, 1 row affected (0.01 sec) Rows matched: 4 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) |
然后重启服务:
1 2 3 4 5 6 7 |
hsm@ubuntu:~$ systemctl restart mysql.service ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to restart 'mysql.service'. Authenticating as: hsm,,, (hsm) Password: ==== AUTHENTICATION COMPLETE === 或 |
注意,网上有人提到可以直接使用:
1 2 3 |
root@mlkui:/etc/mysql/mysql.conf.d# mysqladmin password YourPassword mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. |
设置root密码,实际不起作用!
如果需要从非127.0.0.1的IP地址上连接的话,需要让MySQL绑定到对应的IP地址上:
1 2 3 4 5 6 7 8 9 10 11 |
hsm@ubuntu:/etc/mysql$ ll total 40 drwxr-xr-x 4 root root 4096 Apr 3 03:46 ./ drwxr-xr-x 135 root root 12288 Apr 3 16:33 ../ drwxr-xr-x 2 root root 4096 Apr 3 03:45 conf.d/ -rw------- 1 root root 317 Apr 3 03:46 debian.cnf -rwxr-xr-x 1 root root 120 Jan 28 20:18 debian-start* lrwxrwxrwx 1 root root 24 Apr 3 03:45 my.cnf -> /etc/alternatives/my.cnf -rw-r--r-- 1 root root 839 Aug 3 2016 my.cnf.fallback -rw-r--r-- 1 root root 682 Jan 12 2018 mysql.cnf drwxr-xr-x 2 root root 4096 Apr 3 03:46 mysql.conf.d/ |
可以看到默认是绑定到127.0.0.1的:
1 2 |
hsm@ubuntu:~$ netstat -ano|grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN off (0.00/0/0) |
修改配置文件中的bind-address配置为0.0.0.0(绑定到所有IP地址):
1 2 |
hsm@ubuntu:/etc/mysql$ sudo vim mysql.conf.d/mysqld.cnf hsm@ubuntu:/etc/mysql$ sudo service mysql restart |
此时,依然不能远程访问,报错形如:
1 |
Host '192.168.2.50' is not allowed to connect to this MySQL server |
这是因为用户默认的Host是localhost:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
mysql> use mysql; Database changed mysql> select Host,User from user; +-----------+------------------+ | Host | User | +-----------+------------------+ | localhost | debian-sys-maint | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 4 rows in set (0.00 sec) mysql> update user set host = '%' where user = 'root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) |
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Ubuntu 18.4安装MySQL 5.7