Tag Archives: MYSQL Run sql Script Error

[Linux Docker Mirror] MYSQL Run sql Script Error: Failed to open file ‘/home/mydatabase.sql‘, error: 2

Failed to open file ‘/ home/mydatabase. SQL’, error: 2 when running SQL script in docker image MySQL in Linux

Today, an error occurred when using docker in centos7 to start MySQL and run SQL script files. The error information is as follows:

mysql> source /home/mydatabase.sql;
ERROR: 
Failed to open file '/home/mydatabase.sql', error: 2

After checking the Internet, the cause of the error should be the problem of path matching. The default path is the installation path of MySQL, so MySQL can only access all directories and files under its source directory. The solution is to directly specify the SQL script file to run when logging in. The specific steps are as follows:
first log in to MySQL and create the database to be used. The name of the database I created is Mydatabase1

root@4994c041aa3a: mysql -uroot -p 
Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.36 MySQL Community Server (GPL)
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> create database mydatabase1;

Exit MySQL and log in again by running SQL script files

# Here mydatabase1 is the database created in the first step, and the file after'<' is the path of the sql script file to be run
root@4994c041aa3a: mysql -uroot -p mydatabase1 < /home/mydatabase.sql

log in again and find that the file has run successfully

root@4994c041aa3a: mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.36 MySQL Community Server (GPL)
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| mydatabase1        |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use mydatabase1;
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> show tables;
+-----------------------------+
|    Tables_in_mydatabase1    |
+-----------------------------+
| t_disk                      |
| t_crl                       |
| t_menu                      |
| t_user                      |
+-----------------------------+
4 rows in set (0.00 sec)

Supplement:

# Docker start MySQL command under CentOS7
docker pull mysql:5.7
docker run -tid --name mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:5.7
docker exec -it mysql /bin/bash
mysql -uroot -p
Enter password: