I've managed to get into MySQL using the command line terminal, but when I tried to enter some SQL, it said 'no database selected'
how do I select a database? my database name is: photogallery
What code do I use to select it?
Use USE. This will enable you to select the database.
USE photogallery;
12.8.4: USE Syntax
You can also specify the database you want when connecting:
$ mysql -u user -p photogallery
While invoking the mysql CLI, you can specify the database name through the -D option. From mysql --help:
-D, --database=name Database to use.
I use this command:
mysql -h <db_host> -u <user> -D <db_name> -p
Switch to a database.
mysql> use [db name];
MySQL Commands
Hope this helps.
use [YOUR_DB_NAME];
Alternatively, you can give the "full location" to the database in your queries a la:
SELECT photo_id FROM [my database name].photogallery;
If using one more often than others, use USE. Even if you do, you can still use the database.table syntax.
Use the following steps to select the database:
mysql -u username -p
it will prompt for password, Please enter password.
Now list all the databases
show databases;
select the database which you want to select using the command:
use databaseName;
select data from any table:
select * from tableName limit 10;
You can select your database using the command use photogallery;
Thanks !
USE database_name;
eg. if your database's name is gregs_list, then it will be like this >>
USE gregs_list;
Related
We have a queueing system that was developed by our previous developer, and the truncate command was manually executed to the mysql query. We cant use event scheduler on cpanel so the best option we have is to use cronjob. however, we have no idea on how to execute linux command.
Can someone help me to make a linux query for this?
TRUNCATE TABLE counter_logs_vxphl;
Try to execute the command using the following command, replace {USER} with the username and {PASSWORD} with the password, if you want to specify the host just add -h {HOSTNAME}
mysql -u {USER} -p{PASSWORD} -e "TRUNCATE TABLE counter_logs_vxphl;" > mysql-truncate.log
I need some help, why is it that after importing an mysqldump (table) at first you can see result, but when you exit
mysql -uroot -proot
and select again the table then check, it returns empty.
first connect mysql by below command-
mysql -uroot -proot
Note: assuming root is password of root user.
Now connect to database in which you imported table-
use my_db;
Now check your table by-
show tables;
or
show tables like 'my_table'
If still getting error then show how you import data and show first few lines of your backup if possible.
Or start mysql command shell using:
>mysql -u youruser -p yourdatabase
and then check up your table.
mysql>select * from yourtable;
P.S If you didn't choose any database you get appropriate error 1046:
No database selected.
If you don't get such error message and see you table empty YOU CHOSE WRONG DATABASE.
I am trying to restore database from .sql file , i have created the database in phpmyadmin and also using the create if not exist command in the .sql file which i am restoring to the database and both names of database are same in phpmyadmin and .sql file which is"mydatabase".
Here is the command which i am using to restore database.
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
When i execute the above command i am getting the following error, i have also given all the permission to the user upon this database.
ERROR 1049 (42000): Unknown database 'mydatabasename'
If dump file contains:
CREATE DATABASE mydatabasename;
USE mydatabasename;
You may just use in CLI:
mysql -uroot –pmypassword < mydatabase.sql
It works.
Whatever the name of your dump file, it's the content which does matter.
You need to check your mydatabase.sql and find this line :
USE mydatabasename;
This name does matter, and it's the one you must use in your command :
mysql -uroot -pmypassword mydatabasename<mydatabase.sql;
Two options for you :
Remove USE mydatabasename; in your dump, and keep using :
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
Change your local database name to fit the one in your SQL dump, and use :
mysql -uroot -pmypassword mydatabasename<mydatabase.sql;
Open the sql file and comment out the line that tries to create the existing database and remove USE mydatabasename and try again.
You can also create a database named 'mydatabasename' and then try restoring it again.
Create a new database using MySQL CLI:
mysql -u[username] -p[password]
CREATE DATABASE mydatabasename;
Then try to restore your database:
mysql -u[username] -p[password] mydatabase<mydatabase.sql;
I solved because I have the same problem and I give you some clues:
1.- As #eggyal comments
mydatabase != mydatabasename
So, check your database name
2.- if in your file, you want create database, you can't set database that you not create yet:
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
change it for:
mysql -uroot -pmypassword <mydatabase.sql;
Create database which gave error as Unknown database,
Login to mysql shell:
sudo mysql -u root -p
create database db_name;
Now try restoring database using .sql file, -p flag will ask for a sql user's password once command is executed.
sudo mysql -u root -p db_name < db_name.sql
La Chi's answer works for me.
You can view his/her answer in the comment of zessx answer in this page. But I initially have a problem with it if you also do just tweak his/her answer like this: mysql -h localhost -u root -p -D mydatabase < mydatabase.sql.
Also I would suggest that in the mydatabase.sql portion you include the direct location of it in your computer like "C:\Users\username\desktop".
Thank you.
If initially typed the name of the database incorrectly. Then did a Php artisan migrate .You will then receive an error message .Later even if fixed the name of the databese you need to turn off the server and restart server
I had the same issue, i run this command on command line and just like you i had added the ';' at the end. Removing it solved the issue.
Instead of this
mysql -uroot -pmypassword mydatabase<mydatabase.sql;
try this
mysql -uroot -pmypassword mydatabase<mydatabase.sql
I found these lines in one of the .sql files
"To connect with a manager that does not use port 3306, you must specify the port number:
$mysqli = new mysqli('127.0.0.0.1','user','password','database','3307');
or, in procedural terms:
$mysqli = mysqli_connect('127.0.0.0.1','user','password','database','3307');"
It resolved the error for me . So i will suggest must use port number while making connection to server to resolve the error 1049(unknown database).
mysql -uroot -psecret mysql < mydatabase.sql
I meet your issue. This is how to solve it
Check your DB name correct and exist in MySQL
Check if your IP and port is correct
It works by creating database and than typing command as :
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p -D cricket < C:\Users\habib_s9ayvfl\Desktop\sqlfile.sql
Create database:
CREATE DATABASE mydatabasename;
USE mydatabasename;
use this one:
mysql -u root -p 'mydatabasename'< '/tmp/db_dump.sql'
Its very simple: Use mysql -u root -p mysql
first, you need to check the folder /var/lib/mysql for mydatabasename (depend on how you installed mysql, but default folder is this one),
please check the folder exists or not and its owner should be mysql:mysql, and of course the folder permission should be rw to mysql;
second, possibly because of you made changes to /etc/my.cnf, for example in my case, we created a database TEST_DB in uppercase, and then someone added lower_case_table_names=1 restriction in my.cnf, it caused the Unknown database error because mysql will transalte TEST_DB to lowercase test_db even when i key in select from TEST_DB, so it'll never find TEST_DB, simply comment out and restart mysql service solved my issue
You can also try
> mysql mysql
and you will connect to MySQL database from which you can create your own schema.
mysql> CREATE DATABASE mydb; USE mydb;
when u import database from workbench or other method ,should be give same name as your dump to avoid this kind of error
I want to execute a text file containing SQL queries, in MySQL.
I tried to run source /Desktop/test.sql and received the error:
mysql> . \home\sivakumar\Desktop\test.sql ERROR: Failed to open file
'\home\sivakumar\Desktop\test.sql', error: 2
Any idea on what I am doing wrong?
If you’re at the MySQL command line mysql> you have to declare the SQL file as source.
mysql> source \home\user\Desktop\test.sql;
You have quite a lot of options:
use the MySQL command line client: mysql -h hostname -u user database < path/to/test.sql
Install the MySQL GUI tools and open your SQL file, then execute it
Use phpmysql if the database is available via your webserver
you can execute mysql statements that have been written in a text file using the following command:
mysql -u yourusername -p yourpassword yourdatabase < text_file
if your database has not been created yet, log into your mysql first using:
mysql -u yourusername -p yourpassword
then:
mysql>CREATE DATABASE a_new_database_name
then:
mysql -u yourusername -p yourpassword a_new_database_name < text_file
that should do it!
More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
My favorite option to do that will be:
mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"
I use it this way because when you string it with "" you avoiding wrong path and mistakes with spaces and - and probably more problems with chars that I did not encounter with.
With #elcuco comment I suggest using this command with [space] before so it tell bash to ignore saving it in history, this will work out of the box in most bash.
in case it still saving your command in history please view the following solutions:
Execute command without keeping it in history
extra security edit
Just in case you want to be extra safe you can use the following command and enter the password in the command line input:
mysql --user="username" --database="databasename" -p < "filepath"
All the top answers are good. But just in case someone wants to run the query from a text file on a remote server AND save results to a file (instead of showing on console), you can do this:
mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file
Hope this helps someone.
I came here searching for this answer as well, and here is what I found works the best for me: Note I am using Ubuntu 16.x.x
Access mysql using:
mysql -u <your_user> - p
At the mysql prompt, enter:
source file_name.sql
Hope this helps.
Give the path of .sql file as:
source c:/dump/SQL/file_name.sql;
mysql> source C:\Users\admin\Desktop\fn_Split.sql
Do not specify single quotes.
If the above command is not working, copy the file to c: drive and try again.
as shown below,
mysql> source C:\fn_Split.sql
instead of redirection I would do the following
mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'
This will execute the file path-to-sql-file
Never is a good practice to pass the password argument directly from the command line, it is saved in the ~/.bash_history file and can be accessible from other applications.
Use this instead:
mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:
mysql -uusername -ppassword database-name < file.sql
So many ways to do it.
From Workbench: File > Run SQL Script -- then follow prompts
From Windows Command Line:
Option 1: mysql -u usr -p
mysql> source file_path.sql
Option 2: mysql -u usr -p '-e source file_path.sql'
Option 3: mysql -u usr -p < file_path.sql
Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
mysql -u usr -p < file_path.sql
If you get errors from the command line, make sure you have previously run
cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize
This must be run from within the mysqld.exe directory, hence the CD.
Hope this is helpful and not just redundant.
From linux 14.04 to MySql 5.7, using cat command piped with mysql login:
cat /Desktop/test.sql | sudo mysql -uroot -p
You can use this method for many MySQL commands to execute directly from Shell. Eg:
echo "USE my_db; SHOW tables;" | sudo mysql -uroot -p
Make sure you separate your commands with semicolon (';').
I didn't see this approach in the answers above and thought it is a good contribution.
Very likely, you just need to change the slash/blackslash:
from
\home\sivakumar\Desktop\test.sql
to
/home/sivakumar/Desktop/test.sql
So the command would be:
source /home/sivakumar/Desktop/test.sql
use the following from mysql command prompt-
source \\home\\user\\Desktop\\test.sql;
Use no quotation. Even if the path contains space(' ') use no quotation at all.
Since mysql -u yourusername -p yourpassword yourdatabase < text_file did not work on a remote server (Amazon's EC2)...
Make sure that the Database is created first.
Then:
mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql
For future reference, I've found this to work vs the aforementioned methods, under Windows in your msql console:
mysql>>source c://path_to_file//path_to_file//file_name.sql;
If your root drive isn't called "c" then just interchange with what your drive is called. First try backslashes, if they dont work, try the forward slash. If they also don't work, ensure you have your full file path, the .sql extension on the file name, and if your version insists on semi-colons, ensure it's there and try again.
If you are here LOOKING FOR A DRUPAL ENVIRONMENT
You can run with drush command on your project directory
drush sqlc
If you are trying this command :
mysql -u root -proot -D database < /path/to/script.sql
You may get an error like this : if you have special characters, mainly '`'
ERROR 1064 (42000) at line 1: 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 '/path/to/script.sql' at line 1
So I would suggest to use a command like this :
echo "source /path/to/script.sql" | mysql -u root -proot -D database
This command will execute source /path/to/script.sql once connected to the server, which execute your script.
I had this error, and tried all the advice i could get to no avail.
Finally, the problem was that my folder had a space in the folder name which appearing as a forward-slash in the folder path, once i found and removed it, it worked fine.
I use Bash's Here Strings for an instant SQL execution:
mysql -uroot -p <<<"select date(now())"
https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings
I am writing a script to perform tasks between two MySql databases on the same server, i.e truncate tables on one db and import table rows from another db to this one.
The user who is doing the tasks has full permissions on both databases.
How do I connect to both databases from the command line?
Thanks in advance for any help.
You can use mysqlcommand line utility with the proper parameters:
mysql -u root -h your_host -p your_db
Here root is the privileged user and your_db is the database which is in use by default. You can always switch between databases by typing use another_db command from mysqlconsole.
Also note that you do not have to select dabase (use db_name) in order to execute query on it. You can for example write a query something like this:
SELECT a.id, b.title FROM db1.table1 AS a
LEFT JOIN db2.table AS b ON b.id = a.foreign_id
erm, well I would suggest you open up two terminal windows. the command to connect is:
mysql -u DBUSERNAME -h DBSERVER -p DBNAME
assuming you have mysql installed, which for ubuntu would be: sudo apt-get install mysql