尽量在所有命令前加上 sudo
安装
首先,可以查找系统的mysql (适用于mysql5.7版本)
sudo apt search mysql
Sorting... Done
Full Text Search... Done
mysql-server/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 all
MySQL database server (metapackage depending on the latest version)
mysql-server-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
MySQL database server binaries and system database setup
接下来,开始安装
sudo apt-get install mysql-server-5.7
查看mysql是否正常运行
sudo netstat -tap | grep mysql
tcp 0 0 localhost:mysql 0.0.0.0:* LISTEN 29221/mysqld
如果需要安装mysql 8.0版本,则需要额外下载,并使用如下命令安装查看
wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.10-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb
这时候,要执行一遍 sudo apt-get update,会遇到问题,
sudo apt-get update
Get:1 http://repo.mysql.com/apt/ubuntu bionic InRelease [20.0 kB]
Hit:2 http://azure.archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://azure.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://azure.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:5 http://azure.archive.ubuntu.com/ubuntu bionic-security InRelease
Err:1 http://repo.mysql.com/apt/ubuntu bionic InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 467B942D3A79BD29
Reading package lists... Done
W: GPG error: http://repo.mysql.com/apt/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 467B942D3A79BD29
E: The repository 'http://repo.mysql.com/apt/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
分别执行
sudo gpg --keyserver keyserver.ubuntu.com --recv 467B942D3A79BD29
sudo gpg --export --armor 467B942D3A79BD29 | sudo apt-key add -
或者请参见下一篇
然后接着安装
sudo apt-get install mysql-server
安装过程中会让你输入root密码
完后,可以查看mysql 8.0安装状态
mysql -V
sudo systemctl status mysql
ps -el | grep mysqld
连接
使用root帐户登录,默认密码为空
sudo mysql -u root -p
说明:记得带上root,否则会报错
mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
接下来,对数据库进行初始化设置,可以根据自己选择y或No
sudo mysql_secure_installation
这其中会让设置新的密码。
检查mysql状态
sudo systemctl status mysql
可以再次登录数据库,测试设置的新密码
sudo mysql -u root -p
为mysql 5.7 配置远程访问
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
注释该行
bind-address = 127.0.0.1
保存退出后,重新登录进入mysql,授权命令
mysql> grant all on *.* to root@'%' identified by 'root' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
为mysql 8.0配置远程访问
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> flush privileges;
大致如此
重启mysql并查看状态
sudo systemctl restart mysql
sudo systemctl status mysql
至此,可以用mysql workbench/ Navicat/ DBeaver等数据库连接工具连接该mysql。
参见:
Ubuntu18.04 安装MySQL_Weison Wei的博客-CSDN博客_ubuntu 安装mysql
Ubuntu18.04下安装MySQL - OpsDrip - 博客园
null, message from server: "Host 'xxxxxxx' is not allowed to connect to this MySQL server" - 简书