How to run a script against a .sql database on MySQL Workbench

别来无恙 提交于 2021-01-28 19:11:43

问题


Sorry for me being a novice, I know this looks like a dumb question. I'm trying to follow this very beginner's tutorial. It seems the instructor is using the employees.sql database from this sample database. If I just run the command:

select * from employees;

I get the error

Error Code: 1046. No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.

I tried opening the employees.sql on the side:

           

but then running it returns the error message:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source load_departments.dump' at line 1

And this similar issue on their GitHub repository also doesn't help much. I tried following the installation instructions on the sample database repository:

mysql < employees.sql

however, I get another error:

ERROR 1045 (28000): Access denied for user 'foobar'@'localhost' (using password: NO)

I would appreciate it if you could help me know what are the prerequisites in the step by step form to be able to follow the rest of this tutorial.

P.S. I'm on macOS Catalina and my Workbench version is 8.0, and MySQL

mysql Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)

following the instructions here.


回答1:


There is a small error in that repository.

You'll have to fix that.

Open up the employees.sql with an editor, like Notepad++ for example then go to the bottom of the file where you will find this:

SELECT 'LOADING departments' as 'INFO';
source load_departments.dump ;
SELECT 'LOADING employees' as 'INFO';
source load_employees.dump ;
SELECT 'LOADING dept_emp' as 'INFO';
source load_dept_emp.dump ;
SELECT 'LOADING dept_manager' as 'INFO';
source load_dept_manager.dump ;
SELECT 'LOADING titles' as 'INFO';
source load_titles.dump ;
SELECT 'LOADING salaries' as 'INFO';
source load_salaries1.dump ;
source load_salaries2.dump ;
source load_salaries3.dump ;

source show_elapsed.sql ;

Try replacing:

"SELECT 'LOADING departments' as 'INFO';
source load_departments.dump ;"

with

"SELECT 'LOADING departments' as 'INFO';
source C:/load_departments.dump ;
  • Where C:/ should be the actual path to the file in question, on your computer.
  • Do this for every line in the file there at the end, adding the appropriate path.

Now import employees.sql and run it and see if it works.

If it doesn't, then try doing it this way:

Delete only the following code and save the changes to the file.

SELECT 'LOADING departments' as 'INFO';
source load_departments.dump ;
SELECT 'LOADING employees' as 'INFO';
source load_employees.dump ;
SELECT 'LOADING dept_emp' as 'INFO';
source load_dept_emp.dump ;
SELECT 'LOADING dept_manager' as 'INFO';
source load_dept_manager.dump ;
SELECT 'LOADING titles' as 'INFO';
source load_titles.dump ;
SELECT 'LOADING salaries' as 'INFO';
source load_salaries1.dump ;
source load_salaries2.dump ;
source load_salaries3.dump ;

Now continue doing this:

  • Open Workbench

  • Add a new MySQL connection to your local MySQL server.

Make sure you are connecting/adding the root user or another user that has the following privileges, to the connection:

SELECT, INSERT, UPDATE, DELETE, 
CREATE, DROP, RELOAD, REFERENCES, 
INDEX, ALTER, SHOW DATABASES, 
CREATE TEMPORARY TABLES, 
LOCK TABLES, EXECUTE, CREATE VIEW
  • Double click and open your newly formed connection

  • Go to Left-Upper corner, click on File then click on Open SQL Script and select the employees.sql from the repository you downloaded and click the lightning icon.

After that:

  • use the Open SQL Script again and open load_employees.dump then click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_departments.dumpthen click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_dept_emp.dumpthen click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_dept_manager.dumpthen click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_titles.dump then click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_salaries1.dumpthen click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_salaries2.dumpthen click the Lightning icon again in the newly opened file.

  • use the Open SQL Script again and open load_salaries3.dump then click the Lightning icon again in the newly opened file.

Now you're done importing the database. You can now do your select * from employees query.




回答2:


OK, I think I have found the solution, and it requires a little bit of command line. If you have followed these instructions to install MySQL and MySQL Workbench using HomeBrew, Then you should already have a user and a password set. Now assuming that you have downloaded this GitHub repository, The you need to go to the downloaded test_db-master and following the instructions here, from your terminal and run the command:

mysql -u <user_name> -p -t < employees.sql

where you replace the <user_name> with the user you want to use. In my case, it is just root. Then it asks for the user's password and you should see the database imported. Now lunch the MySQL workbench. And you should see the employees database in the schemas tab of the left-hand sidebar/panel. Now you need to double click on it, or right-click on it and select set as default schema:

       

alternatively, you could add

USE employees;

at the top of your script for the first time. Now you are good to go and can run the rest of the queries, following the tutorial.



来源:https://stackoverflow.com/questions/60866885/how-to-run-a-script-against-a-sql-database-on-mysql-workbench

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!