1 ubuntu下mysql 8.0.22安装 并导入sakila 数据

it2025-07-12  5

1. 添加mysql apt源

把MySQL apt源添加至系统的软件仓库列表里,需要下载mysql-apt-config包; 下载页面:https://dev.mysql.com/downloads/file/?id=487007 下载得到mysql-apt-config_0.8.15-1_all.deb文件

2 安装mysql

sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb // 安装deb文件 sudo apt-get update // 更新源 sudo apt-get install mysql-server

过程中需要设置安装版本,设置密码

3 mysql服务命令

a. 启动服务

sudo service mysql start

b. 停止服务

sudo service mysql stop

c. 检查服务状态

luslin@local:/var/log$ systemctl status mysql.service ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since 三 2020-10-21 18:04:48 CST; 14h ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Main PID: 26527 (mysqld) Status: "Server is operational" Tasks: 40 Memory: 316.5M CPU: 2min 22.364s CGroup: /system.slice/mysql.service └─26527 /usr/sbin/mysqld 10月 21 18:04:46 local systemd[1]: Starting MySQL Community Server... 10月 21 18:04:48 local systemd[1]: Started MySQL Community Server. 10月 21 18:05:27 local systemd[1]: Started MySQL Community Server. 10月 21 18:18:17 local systemd[1]: Started MySQL Community Server. 10月 21 18:21:19 local systemd[1]: Started MySQL Community Server.

4 设置远程访问权限

luslin@local:/var/log$ mysql -u root -p // 登录 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 31 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> 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> select User,authentication_string,Host from user; // 查询用户与密码 +------------------+------------------------------------------------------------------------+-----------+ | User | authentication_string | Host | +------------------+------------------------------------------------------------------------+-----------+ | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | % | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost | | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | localhost | +------------------+------------------------------------------------------------------------+-----------+ 5 rows in set (0.00 sec)

Host 为localhost 的只能本地连接。 %的则本地与远程均可

创建数据库,与用户,并给访问权限

create database sakila; create user sakila@'%' identified WITH mysql_native_password BY 'sakila'; // 由于要使用mysql workbench 连接 grant all privileges on sakila.* to sakila@'%' with grant option; // 授予数据库访问权限 flush privileges;

5 导入数据

a. 下载数据 数据下载地址:https://dev.mysql.com/doc/index-other.html Example Databases 下面的sakila database 下载ZIP文件。 解压后得到三个文件

luslin@local:~/下载/sakila-db$ ls sakila-data.sql sakila.mwb sakila-schema.sql

在在sql中导入:

mysql> source /home/luslin/下载/sakila-db/sakila-schema.sql mysql> source /home/luslin/下载/sakila-db/sakila-data.sql

导入时出现错误

ERROR 13 (HY000): Can't get stat of './sakila' (OS errno 13 - Permission denied)

发现是目录权限错误,将/var/lib下mysql文件夹的权限设置成mysql就好了

cd /var/lib sudo chown -R mysql:mysql mysql
最新回复(0)