Ubuntu安装MySQL

Ubuntu(在线版)

更新软件源

sudo apt-get update

安装MySQL

sudo apt-get install mysql-server

启动服务

systemctl start mysql

查看MySQL状态

systemctl status mysql

看到activate即启动成功

Ubuntu(离线版)

进入下载链接选择MySQL版本下载

https://dev.mysql.com/downloads/mysql/

注:本文章下载mysql-server_8.0.35-1ubuntu20.04_amd64.deb-bundle.tar

解压文件到指定目录

tar -xvf mysql-server_8.0.35-1ubuntu20.04_amd64.deb-bundle.tar -C /usr/local/mysql

把该文件解压到usr/local/mysql当中

安装MySQL

cd /usr/local/mysql
sudo dpkg -i mysql-server_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-community-client-core_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-common_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-community-client_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i libmysqlclient21_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i libmysqlclient-dev_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-client_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-community-server-core_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-community-server_8.0.35-1ubuntu20.04_amd64.deb
sudo dpkg -i mysql-server_8.0.35-1ubuntu20.04_amd64.deb

可能会出现报错:

dpkg: dependency problems prevent configuration of mysql-community-server-core:
 mysql-community-server-core depends on libaio1 (>= 0.3.93); however:
  Package libaio1 is not installed.
 mysql-community-server-core depends on libmecab2 (>= 0.996); however:
  Package libmecab2 is not installed.

报错原因是缺少依赖libaio1和libmecab2,安装即可

下载地址:

libaio1

http://archive.ubuntu.com/ubuntu/pool/main/liba/libaio/libaio1_0.3.110-5_amd64.deb

libmecab2

http://archive.ubuntu.com/ubuntu/pool/universe/m/mecab/libmecab2_0.996-5_amd64.deb

sudo dpkg -i libaio1_0.3.110-5_amd64.deb
sudo dpkg -i libmecab2_0.996-5_amd64.deb

 在执行命令 sudo dpkg -i mysql-community-server_8.0.35-1ubuntu20.04_amd64.deb 时可能会出现设置密码的界面,正常设置即可。

启动服务

systemctl start mysql

查看MySQL状态

systemctl status mysql

看到activate即启动成功

初始化密码

如果安装时没有设置密码(尤其是在线安装),则MySQL无法正常使用,故需要将密码重置。

安全模式登录MySQL

sudo mkdir /var/run/mysqld  
sudo chown mysql /var/run/mysqld
sudo mysqld_safe --skip-grant-tables&   

进入MySQL

sudo mysql --user=root mysql

置空密码

UPDATE mysql.user SET authentication_string=null WHERE User='root';
flush privileges;  

修改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

 退出MySQL

quit;

再次进入MySQL可以使用设置的密码(‘123456’)进入MySQL

进入MySQL

mysql -u root -p

输入密码后进入MySQL命令行。可以使用SQL进行测试

show databases;

该SQL会返回所有的数据库名。

开启外部访问

使用软件(如DataGrip,navicat等)访问MySQL,需要开启外部访问权限

进入MySQL执行SQL

use mysql;
update user set host='%' where user='root';
flush privileges;

退出MySQL

quit;

安装完毕,可使用命令

修改配置文件

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

将bind-address和mysqlx-bind-address注释掉(没有则不管),再重新启动MySQL

systemctl restart mysql

你可能感兴趣的:(数据库,mysql)