$ mysql> connect ... (from bash) what does it do? - mysql

This is not a issue, just a curiosity which I couldn't find any answer for on mysql's reference or googling:
First of all I know that to connect to a database I would run on bash:
mysql -h myhost -u myusername -p
But if I just run the mysql command without parameters I have the mysql> prompt and on the help I can see that there's a 'connect' command. But you can't set the user or password, just the database and host so it will always deny the access naturally.
I also tried to run mysql with just the username and password parameters from bash (to specify the db and host later) but that don't work either. So, does the connect command work only if you ran mysql with the username and pass which are the same for another host? Or does it have more uses?

This is not secured but you can set mysql password on Unix shell as:
export MYSQL_PWD=secret
# then run mysql client to connect
mysql
mysql> connect somedb localhost
mysql> show tables
See MySQL Manual for list of environment variables
PS: mysql will use your unix user as mysql user for connecting. Alternatively you can set env variable USER on Windows or NetWare.

Related

Use same database with different computers on same lan [duplicate]

I have MySQL setup in a PC on my local network, how do I connect to it?
I also have MySQL installed on this computer (which I want to use to connect to the database).
I tried the following but it's not working:
mysql -u user -h 192.168.1.28:3306 -p password
ERROR 2005 (HY000): Unknown MySQL server host '192.168.1.28:3306' (0)
EDIT
Thanks for your help. Anyway, I connect without 3306 and I have another problem. MACBOOK is the name of my client computer.
mysql -u user -ppassword -h 192.168.1.28
ERROR 1045 (28000): Access denied for user 'user'#'MACBOOK' (using password: YES)
Thanks.
That was a very useful question! Since we need to run the application with a centralized database, we should give the privileges to that computer in LAN to access the particular database hosted in LAN PC. Here is the solution for that!
Go to MySQL server
Type the following code to grant access for other pc:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root_password';
then type:
FLUSH PRIVILEGES;
Replace % with the IP you want to grant access for!
Users who can Install MySQL Workbench on MySQL Server Machine
If you use or have MySQL Workbench on the MySQL Server PC you can do this with just a few clicks. Recommend only for development environment.
Connect to MySQL Server
Find this option Users and Privileges from Navigator and click on it.
Select root user and change value for Limit to Hosts Matching to %.
The click Apply at the bottom.
This should enable root user to access MySQL Server from remote machine.
Since you have MySQL on your local computer, you do not need to bother with the IP address of the machine. Just use localhost:
mysql -u user -p
or
mysql -hlocalhost -u user -p
If you cannot login with this, you must find out what usernames (user#host) exist in the MySQL Server locally. Here is what you do:
Step 01) Startup MySQL so that no passwords are require no passwords and denies TCP/IP connections
service mysql restart --skip-grant-tables --skip-networking
Keep in mind that standard SQL for adding users, granting and
revoking privileges are disabled.
Step 02) Show users and hosts
select concat(''',user,'''#''',host,'''') userhost,password from mysql.user;
Step 03) Check your password to make sure it works
select user,host from mysql.user where password=password('YourMySQLPassword');
If your password produces no output for this query, you have a bad
password.
If your password produces output for this query, look at the users
and hosts. If your host value is '%', your should be able to connect
from anywhere. If your host is 'localhost', you should be able to
connect locally.
Make user you have 'root'#'localhost' defined.
Once you have done what is needed, just restart mysql normally
service mysql restart
If you are able to connect successfully on the macbook, run this
query:
SELECT USER(),CURRENT_USER();
USER() reports how you attempted to authenticate in MySQL
CURRENT_USER() reports how you were allowed to authenticate in
MySQL
Let us know what happens !!!
UPDATE 2012-02-13 20:47 EDT
Login to the remote server and repeat Step 1-3
See if any user allows remote access (i.e, host in mysql.user is '%'). If you do not, then add 'user'#'%' to mysql.user.
Follow a simple checklist:
Try pinging the machine ping 192.168.1.2
Ensure MySQL is running on the specified port 3306 i.e. it has not been modified.
Ensure that the other PC is not blocking inbound connections on that port. If it is, add a firewall exception to allow connections on port 3306 and allow inbound connections in general.
It would be nice if you could post the exact error as it is displayed when you attempt to make that connection.
mysql -u user -h 192.168.1.2 -p
This should be enough for connection to MySQL server.
Please, check the firewall of 192.168.1.2 if remote connection to MySQL server is enabled.
Regards
In Ubuntu Follow these steps:
Set bind-address at /etc/mysql/mysql.conf.d
Change bind-address = 127.0.0.1 to bind-address = 192.24.805.50 # your IP
Grant permission for the remote machine
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'#'[remoteip]' IDENTIFIED
BY 'anypassword' WITH GRANT OPTION;
Then try connect from remote machine
mysql -u root -h 192.24.805.50 -p
Connecting to any mysql database should be like this:
$mysql -h hostname -Pportnumber -u username -p (then enter)
Then it will ask for password. Note: Port number should be closer to -P or it will show error. Make sure you know what is your mysql port. Default is 3306 and is optional to specify the port in this case. If its anything else you need to mention port number with -P or else it will show error.
For example:
$mysql -h 10.20.40.5 -P3306 -u root -p (then enter)
Password:My_Db_Password
Gubrish about product you using.
mysql>_
Note: If you are trying to connect a db at different location make sure you can ping to that server/computer.
$ping 10.20.40.5
It should return TTL with time you got back PONG.
If it says destination unreachable then you cannot connect to remote mysql no matter what.
In such case contact your Network Administrator or Check your cable connection to your computer till the end of your target computer. Or check if you got LAN/WAN/MAN or internet/intranet/extranet working.
actually you shouldn't specify port in the host name. Mysql has special option for port (if port differs from default)
kind of
mysql --host=192.168.1.2 --port=3306
You don't have to specify ':3306' after the IP, it's the default port for MySQL.
And if your MySQL server runs with another port than 3306, then you have to add '-P [port]' instead of adding it to the IP address.
The MySQL client won't recognize the syntax "host:port", you HAVE to use -P [port] instead.
And btw, if you use '-p password', it won't work and will ask you the password again. You have to stick the password to the -p : -ppassword. (still, it's a very bad habit, because anyone that could do a PS on your server could see the plain password...)
You should use this:
>mysql -u user -h 192.168.1.2 -P 3306 -ppassword
or this:
>mysql -u user -h 192.168.1.2 -ppassword
...because 3306 is a default port number.
mysql Options

how to logon to mysql as a non localhost user

I have two users set up, one is testuser#localhost and one is testuser#111.111.11.111 for example. When I do the following command from ubuntu 20.04 prompt:
mysql -u testuser -p it prompts me for a password and logs me on to testuser#localhost. If I try something like mysql -u 'testuser'#'111.111.11.111' -p it assumes it is localhost and gives an error message Access denied for user 'testuser#111.111.11.111'#'localhost'. How do I specify and log on as a non localhost user?
I found the command, it is mysql -h 111.111.11.111 -u testuser -p. For some reason I am not able to logon with the user with the ip address specified. I keep getting a 2003 error which indicates some kind of configuration issue. Can't connect to MySQL server on ' server ' (10061) indicates that the network connection has been refused.
Let me edit this to make more sense in what I am trying to do. Eventually there will be two mysql servers, say one on ip address 111.111.11.111 and one on 111.111.11.112 for example. I want to be able from a php script running on 111.1111.11.111 be able to access data from a table on 111.111.11.112 for example. I am just now first trying to logon to the first server which is running a mysql database using the user name and ip address of the host.
I will post the answer to the original question which is to include a host parameter in the logon command as follows:
mysql -h 111.111.11.111 -u testuser -p

Mysql ERROR : not connected

I am trying to connect to MySQL database from MySQL shell on windows.
No matter what I type in MySQL shell, it keeps giving me error : 'Not connected'.
Query eg 1 : mysql --host=localhost --port=3306 --user=root -p;
Query eg 2 : mysql -u root -p
O/P : ERROR: Not connected
I have MySQL server installed on my machine. Also MySQL service is running in the background.
Also, I was able to connect from MySQL workbench.
ERROR MESSAGE
MySQL Workbench Connection
My temporary workaround is that I make use of ssl protocol to connect to MySQL server :
MySQL> \connect root#localhost
MySQL localhost:33060+ ssl SQL > show databases;
The first step is that you need to check if you are in the MYSQL Shell SQL mode or JS mode.
Then if you are in SQL mode then you are good to go else you need to switch to SQL mode by this command
\sql
The next step is to connect using this command
\connect root#localhost
In your case, you might have given the privilege as the IP address so you need to check your localhost IP which can be done by this command in your command prompt.
ipconfig and then just check the IP address and put it in place of localhost in the previous command. If this still doesn't works then put 127.0.0.1:3306.
After this, it will prompt to add or save the password , enter a unique password there.
After this you are good to go and check the user and localhost after this by this command
SELECT user, host FROM mysql.user;
Try mysql -u root -p
I haven't used MySQL shell, I typically use gitbash and it works just fine
I had faced the same issue on my Windows 10 machine with MySQL 5.7 and the following commands helped me:
mysqlsh.exe - to open mysql shell; then
\sql - to start working with SQL;
finally:
\connect root#127.0.0.1:3306
You can use:
mysql -uroot -hlocalhost -P3306 -p
or
mysql -uroot -h127.0.0.1 -P3306 -p
or
mysql -uroot -p

Can we execute sql query on remote machine without logging in

If my box ip is 10.10.10.10, is it possible to execute some sql suery like
select * from some_table
on remote machine 10.10.10.20 without logging into that machine from terminal
If not, when is it possible
Yes, that is possible using the MySQL client. You can either use this syntax:
mysql -u <username> -p -h remote.host database -e 'show databases;'
or use pass in a script file:
mysql -u <username> -p -h remote.host database < script.sql
Yes, it is possible. You have not told us what your setup is. If you are not using a programming language already, see #Jaco's answer. If you are using a programming language, then in your connection string, you replace localhost (or 10.10.10.10) with 10.10.10.20.
If mysql on 10.10.10.10 has networking enabled (by default it is only enabled for local communications, you should enable listening on 0.0.0.0 in config) and port 3306 is open on firewall, you can.
Add a user, that is permitted to connect to mysql remotely from 10.10.10.20 (i.e. username#'10.10.10.20' or username#'%'), then just do
mysql -u username -p -h 10.10.10.10 your_database_name
First of all you need to connect with your remote database no matter where it is located (even for local databases) by providing the server location/url, DB name, user name and the password (in some cases you need to give the port name also). the exact coding you need to use to create the connection differs slightly based on the language you are working with. Once you have a working DB connection with you, you can execute any sql query that is permitted for the logged in user.
If you want to connect from a remote location, the DB user should have privileges to connect remotely. ("%" is used to allow from any location).
CREATE USER 'newuser'#'%' IDENTIFIED BY 'mypass';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'#'%';
if you want to connect from the terminal ;
shell> mysql -h 10.10.10.10 -u newuser -pmypass mydb

MySQL Error:10061

Apparently, I cannot connect to SQL server using the mysql.exe
I logged in as the root user and typed the following
mysql -u root -p
mysql> CONNECT TO 127.0.0.1:3306;
I receive the folling error.
ERROR 2005 (HY000): Unknown MySQL server host '127.0.01:3306' (2)
Unknown MySQL server host '127.0.0.1:3306' (2)
However it connects justs fine using MySQL Workbench with the same parameters.
Host:127.0.0.1
Port:3306
User: root
pass:[empty]
I have the easyphp MySQL module installed. Could this be the reason?
EDIT: TYPO with 127.0.0.1 sorry
According to the documentation, the syntax of the connect command is:
connect [db_name host_name]], \r [db_name host_name]]
Reconnect to the server. The optional database name and host name arguments may be given to specify the default database or the host where the server is running. If omitted, the current values are used.
Therefore your command CONNECT TO 127.0.0.1:3306 is attempting to connect to a database called TO on a host called 127.0.0.1:3306. The error message you receive in return unsurprisingly complains that the host does not exist.
However, it is more usual to specify the hostname and database on invoking mysql (whereupon one can also specify the port if one wishes - see this page for a full list of command line options):
mysql -u username -p -h <hostname> -P <port> db_name
Also note that if the hostname and port are not specified, they default to localhost and 3309 - therefore in your case you can omit all of the above and just go with:
mysql -u username -p db_name
To do what you're currently doing (not specifying the database name on the command-line), you must call the USE command at the mysql> prompt to select a database after you've connected:
mysql -u username -p
mysql> USE db_name;
127.0.01 is explicitly misspelled. 127.0.0.1 is correct
127.0.01:3306 IS NOT 127.0.0.1:3306
You forgot the dot.