how to change Mysql server hostname - mysql

I run my service in docker-compose. And MySQL host name is “mysql”, when I develop in my computer, MySQL host name is “localhost”.
Now I want to change my computer MySQL hostname to “mysql”. So I can connect MySQL on my computer by mysql+pool://root:password#mysql/test_db.
Please help me, I find solution spend lots of time.

It is recommended to separate this connection string setting to an environment variable.
If you really want to use mysql, you can modify your OS's hosts file (e.g. for macOS or many linux distros, it should be /etc/hosts).
and put 127.0.0.1 mysql at the end of the file.

Related

MySql server not showing proper databases from Ubuntu server

I'm using wsl2 on a windows machine. I want to view my databases that I have on mysql server ubuntu in a GUI such as mysql workbench (on windows) but it seems as the two are not linked. In the pictures provided you can see that when I login to root, it displays different databases, I also use different passwords for root on both servers. When I try to use the root password from the ubuntu server in workbench, I get the error that I cannot connect to the database server.
Ubuntu databases
MySql workbench databases
MySql workbench config
MySql workbench error
UPDATE 2022
I found myself in this same need, and found a good resource that tackles this issue rather nicely. The solution itself predates even this question, funnily enough.
Long story short, check the following GitHub repository. Instructions are available and I can confirm it works on Windows 10.0.19041.1415 and WSL2.
https://github.com/shayne/go-wsl2-host
========================================================
WSL doesn't use the same IP as Windows, meaning you can't access it using localhost. Also, WSL IP changes everytime you boot it, meaning that the credentials for the connection will work only once.
In the sister community SuperUser, this has been discussed and some workarounds are avaliable, but I can't tell if they will work specifically with MySQL Workbench, as they ofter require you to use PowerShell/CMD.
Please, refer to the following discussions, which also provide further sources on the topic (There is one in particular that might be useful if you are running Windows 10 Pro).
Make IP address of WSL2 static
localhost and 127.0.0.1 working but not ip address in wsl windows 10
There are several requests to allow us to set WSL IP statically, so we can register it as a host in Windows DNS Host file and use that alias instead of the IP while setting up a connection (or use the IP itself, since it would be static anyway), but it is not ready yet AFAIK.
After reading the answer from #Jetto, I thought you could create a batchfile like this:
#ECHO OFF
wsl export wsl=$(hostname -I); sed -i -e "s/172.[0-9]*.[0-9]*.[0-9]*/${wsl/ /}/g" /mnt/c/Users/*username*/AppData/Roaming/MySQL/Workbench/connections.xml
This will replace the ip-address to the current ip-address of your wsl instance (relying on the fact that is starts with 172.)
If you start MySQL Workbench after running this script, you should be able to connect to MySQL (or MariaDB) which is running in the WSL2 session.
Disclaimer: I am not responsible for the fact that you did not make a backup of the file connections.xml 😉
P.S. In case you wonder: Yes this instance on my computer uses port 3356. But 3306 should work too if you do not have a local MySQL running.

What software is availble to forward a mysql socket file to a remote server?

I used to run a mysql server on a box where clients would connect to localhost (/var/lib/mysql/mysql.sock), but have subsequently moved the MySQL server to dedicated hardware.
To date, I have successfully been able to use both socat and mysql-proxy to make /var/lib/mysql/mysql.sock forward to the remote server, but I wonder what other options are availble, ideally something that can implement caching would be desirable. I played around with ProxySQL but realised that wasn't for me, as it would need a list of all users.
As mysql-proxy was only ever declared alpha I'd like to use something else.

How exactly am I accessing the host/server through localhost on phpMyAdmin?

I am working on a school project in which I have to upload data onto a database hosted on phpMyAdmin.
What's confusing me right now is that I see "localhost" at the top of the page and before I even query the database I see "Run SQL query/queries on server "localhost":"
Does this mean that I'm hosting a server on my computer and accessing the database through that? Because then I query for "SHOW VARIABLES WHERE Variable_name = 'hostname';" and it returns with hostname = webhost330. (It is hosted on webhost330.asu.edu).
I'm extremely confused about what this means. Thanks for any possible help.
I'm very new to databases so forgive me if I'm missing something simple here.
EDIT: To clarify: I'm not at school, I'm at my house. What kind of implications does this have? In other words, what's the difference between this and hosting a database locally?
When I host a database on my computer through the MySQL command line client, I can create database information and it's stored on my computer in files. Does this mean that it's initially stored on localhost and then that data is used to create files on my computer?
And then the equivalent of that for the webhost330 is that the localhost stores the database initially but then uploads it to the host at phpMyAdmin? That is the primary thing I'm confused about.
This exactly means what you assumed. When connected to localhost, you are connected to local MySQL server on same machine. Using webhost330.... you are connected to that remote MySQL server instance, if that is not the name of your own machine. Your own server can be webhost30.etcetc.
Edit
If your website is hosted here: webhost330.asu.edu then MySQL host either being webhost330.asu.edu or localhost both mean the same local MySQL server on your very machine.
Edit based on your update
If you are at home, then localhost means you are connected to a MySQL server that you have installed on your own computer. and webhost30.etc.etc means you are connected to the database you have at your school. Database do allow remote connectivity and if you are connected to school from home, that's a remote MySQL connection.
To remove your confusion, you should use only localhost in your code. localhost at home will mean the development server which is your home computer, and when you take the same code to webhost30.etc.etc then localhost for that server will mean its own MySQL installation. So localhost will work everywhere as long as you don't want your code to connect to a remote external MySQL database server.
Assuming you're at school, and assuming you're on your school network. Then yes, your machine is probably "webhost330.asu.edu". At the very least webhost330.asu.edu thinks "webhost330.asu.edu" is localhost.
To see what "localhost" refers to in terms of MySQL server, you have to look at the URL you are using to connect to the web server.
For example, if the URL is
http://localhost/phpmyadmin
and this instance of phpMyAdmin tells me that the MySQL server is on localhost, this means that the MySQL server is on my local workstation.
If the URL is http://example.com/phpmyadmin, then localhost will mean that the MySQL server is on the same machine (example.com).

How to show different databases MySQL workbench?

I downloaded MySQL, and installed it correctly.
When I started the MySQL workbench it shows me these databases (I don't remember all of them):
sakila
test
world
It is ok, they are work, I can access them with no problems. But later I installed xampp, after that when start MySQL workbench it shows me different databases:
cdcol
phpmyadmin
test
webauth
I can't access the previous databases.
How to switch between them?, or
view them all?
Not sure whether xampp uninstalled your first server, but it is more likely that its installation changed the default MySQL TCP/IP port (3306). Maybe both servers try to run on the same port (which doesn't work) and one failed to start (in this case the first installation).
So what you need is to make each server run on its own port. You can use MySQL Workbench to make the necessary change. However first you need to check if still both servers are installed. Do you have 2 MySQL services running? Switch off one of them and connect with MySQL to the running server. If that works shut this down and run the other service. Connect again with MySQL but this time go to the admin section, config file, and change the port to e.g. 3307. Save and restart the server (all within WB possible). Now you should be able to run both servers at the same time (provided the first one runs still on the default port). Create a second connection and server instance in WB so that you can access one server at 3306 and the other at 3307
If there's only one MySQL service entry then xamp has removed the previous installation and you must install it again if you really need two servers.

Connect to MySQL database

I need to connect to an SQL database to see how many records it is holding. I cannot access phpmyadmin and no one seems to know the host name or port. All I have is the username and database name.
How can I find the information I need?
If it's on the same computer that you are working on, then the hostname is localhost and the port is default (3306).
...or if you have terminal/console access to the machine the database is running on, run the command:
mysql -u<user> -p<password> -d<database>
to log on to the database.
Find someone, anyone, who can connect to the database you need to connect to.
Then you can examine their setup - and connect to the same place.
Otherwise, you're into port-scanning across the machines in your network. Beware of intrusion detection systems. Be clever about it. And scan your machine(s) first.
if it is on your network you can scan for the default SQL port that Mikhail has referred to.