MySQL database IP address from rackspace or google cloud? - php

Ok here’s the deal. I setup MySQL database on Rackspace and I’m trying to connect to it. I am using the tutorial google maps to create store locator using MySQL. The line of the code in the tutorial asks for the host name and I’m giving the IP address of my server on Rackspace. I’m using what I think is the correct one but it’s not working. Any ideas?
Here's the google tutorial code:
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
};
My code:
$connection=mysqli_connect ('THE IP ADDRESS OF MY RACKSPACE SERVER', 'MY USERNAME', 'MY PASSWORD');
Here's a link to the tutorial from google: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator
I know I have the server IP correct and my username/password correct since I'm able to login via SSH from terminal with those credentials.
When I use mysql> \s to show the status it says Connection: Localhost via UNIX socket - does this mean it's local host? I need it hosted on IP to connect right?

I expect that your MySQL server is by default by your Linux distribution's packaging configured to listen either locally, or on the socket file only. You can update your DB config to listen on the public IP address of your server however obviously this can come with some security implications.
To do this edit the /etc/mysql/mysqld.conf file (this may be in a slightly different location depending on distribution being used) and the following line as such...
From
#bind-address = 127.0.0.1
To
bind-address = 0.0.0.0
Now restart your MySQL service using the systemctl or service command.
service mysql restart
Your MySQL server is now listening on ALL the host's IP addresses. If you want to limit it to just one you should enter that IP instead of 0.0.0.0. You should now be able to connect to your MySQL server remotely, however, you must have already configured your database user to be able to login from the webserver. If you haven't configured the user yet do something like this.
mysql
CREATE USER '<username>'#'<webserver ip address here>' INDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON <database>.* TO '<username>'#'<webserver ip address>';
FLUSH PRIVILEGES;
You should now be able to login as this user and view / modify / insert etc... data to the database specified from the server IP address specified. You can test this from the web server using the MySQL client like this...
mysql -u <username> -h <db server ip> -p

Related

Connecting to a MySQL container in the same pod

Good day,
I am using Kubernetes to run containers on the google container engine.
The idea is to run two containers in a pod. One container uses the docker mysql image, the other runs php, laravel, nginx and composer.
Locally, this works. The idea is that the php can connect to the database on localhost, and this should work if both containers are in the same pod. However, when the pod is launched, we see the following message in the log:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The only difference is that when testing locally, I change localhost to the internal docker ip.
Thanks and good day
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:
shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP
The --protocol option enables you to establish a particular type of connection even when the other options would normally default to some other protocol.
The other solution is using Kubernetes Volume abstraction to share a path between containers. Edit /etc/mysql/my.cnf on both containers' images and change socket location for both MySQL server and client to point to the shared directory or disk.
MySQL comes preconfigured to only support named pipe connections on the localhost, if you are going to connect to it from another logical machine you need to locate the following line in your my.cnf file...
#bind-address = 127.0.0.1
and replace it with...
bind-address = {your network ip here}
Once you have enabled the bind address restart your mysql server, check the error log to ensure that no exceptions occurred with binding the address, and then login as root and create a new user account attached to that ip address which you can then use to access MySQL from any other logical machine as long as you connect to it using the IP address you defined in my.cnf.
Please note by default the root account is only accessible from the localhost so you will either need to create a new user account or alter the root account to support remote connections (not secure)

Access MySql Databases from same Network or Remotely access from Different Servers

I have an application in which I have to access all pc connected to same network and their MySql Databases and I also want want to connect to remotely a server.
Actually I have list of drop down services and each service holding a database name. when I select a service then I want to build connection to database either it lies on same network or any remote server.
Remember, I know the hostname, username, password and dbname. and I am using mysqli_connect function.
I have try multiple options given on web, but all in vain. e.g grant host and user access. But not found any solution can help to solve my problem.
I have try bind-address option in my.conf file but no solution.
Here is my code
For remote Server
$con = new mysqli_connect('xxx xxx xxx:3306', 'username', 'pass', 'dbname');
For local Network
$con = new mysqli_connect('xxxx xxx xxx:3306', 'localhost', '', 'talent');
Error
mysqli::mysqli();(HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. and also get this one earlier Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES).
I am using windows platform. WIndow 8.1
I guess you want to access mysql database installed on different computers on same network as your computer and also on some remote server.
What you need is IP Addresses of all the computers on which the mysql server is installed including the remote server. And on each such computer mysql server remote access should be enabled for your computer's IP Address and mysql user you are using in 'new mysqli_connect()'. See this tutorial.
Also as fvu said check php documentation for 'mysqli_connect' function.
UPDATED
What is SSH?
1. SSH is SecureSHell.
2. Its nothing but a remote login tool or program(like telnet but in secured way).
3. Remote login is logging in to some other computer(known as remote computer) as a user of that computer from your computer.
4. After you successfully logged in to the remote computer via ssh, you can type commands on that remote computer on behalf of the user you are loggen in with.
5. Consider this as if you are sitting in front of your computer and watching the command prompt(terminal in linux and mac) screen of the remote computer.
6. Whataver you will type here will reflect there.
For your knowledge to make you understand the problem:
1. When you install XAMPP or WAMP on your computer it also installs MySQL Server with it.
2. MySQL Server is a process running in the background to which we can request to do
database operations like SELECT, UPDATE, etc.
3. This thing we generally do using 'mysql_connect' or 'mysqli_connect' in php.
4. MySQL Server can have many users and different users have different previledges/permissions. So that MySQL admin user(i.e. root) can control what things are allowed and not allowed for a user.
5. Now while connecting to the mysql server process we need to specify the user credentials. That you specify in mysqli_connect function.
Now let me explain you why you are getting that error:
1. The user credentials you are using either does not exist or not correct or the user has no access to connect to MySQL Server process remotely. i.e. from other computer that the one has MySQL Server installed on. In your case from your computer to computer A or to computer B or remote server.
2. The other reason may be the firewall settings of remote computers.
Solution:
1. For computer A and computer B you don't need ssh you can directly go to the computer and open command prompt on it and type commands.
2. But for remote computer you need to use ssh.
3. Now another problem is you are using Windows so ssh program will not be available to you.
4. You will need to download putty program. Its ssh implementation for windows. Same as ssh only name is different.
5. Take a putty tutorial to connect to remote server.
6. Then you can use this tutorial for granting remote login access to mysql user you are using in mysqli_connect function.
If the server is a local hosted database and you can connect to it from local host and i you are not able to connect to it from a remote machine consider the following options.
There is a firewall in ubuntu that you need to open, you do this by granting access in IPtables.
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT And now we should be able to login to our server from our local machine:
mysql -h255.112.324.12 -uroot -pMyPASSWORD
You need
to grant access to mysql: https://askubuntu.com/questions/159053/mysql-server-not-accessible-from-remote-machine
as root, open your /etc/mysql/my.cnf with your favorite editor look for the [mysqld] section, and in there for the bind-address keyword. This usually is set to 127.0.0.1 -- change that to match your "normal" IP-address save the file, and reload the service (e.g. using service mysql restart)
Last but not least you need to give remote access in mysql:
GRANT ALL ON mydb.* TO root#'%' IDENTIFIED BY 'MyPASSWORD';
Find more information here: http://web.archive.org/web/20120930214828/http://chosencollective.com/technology/how-to-enable-remote-access-to-mysql
Right now you seem to be mixing object-oriented and procedural approaches. If you want to use object-orented approach, mysqli object should be instantiated as follows:
$con = new mysqli($host, $username, $password, $dbname, $socket); // all but first parameter are optional
If you want to use procedural approach, mysqli connection resource should be created as follows:
$con = mysqli_connect($host, $username, $password, $dbname, $socket); // all but first parameter are optional

Connecting Local Host to online sql database

I have an online reservation booking system (php script) that uses a mySQL database. Part of the script is a back end admin panel for offline reservations.
Is it possible to run a service like XAMPP using local host to access the remote database.
You can connect to remote database by having remote host, db username, db password.
Host name = (use the db server IP address)
Database name = (cpanelUsername_databaseName)
Database username = (cpanelUsername_databaseUsername)
Database password = (*)
MySQL Connection Port = 3306 (check your)
If you are using cPanel then you need to allow your local IP for DB access first.
Login to cpanel, on the main menu of CPanel,
Jump down to "Databases" and select "Remote MySQL"
Now add your IP address, or IP range with a wildcard such as 12.34.%
Good Luck
Yes. You need login credentials for the remote database as well as it's port and server address (hostname or IP), as you'd expect. The remote host also needs to have permissions granted for that particular user to access the database remotely, which users will not have by default.
Yes it is. Just the way you connect your local database, you can connect to a remote database with valid credentials. Then only thing changes is the hostname parameter.
for an example if your database is hosted on dreamhost, they have hostnames like..mysql.yourdomain.com. Create a database on your remote server and connect to it as
<?php
mysql_connect('mysql.yourdomain.com','username','password');

cant connect database server using ip address

I am using PHP and MySQL to make a database connection , problem is :
the web application project in my local machine when am trying connect to database remote server every thing is work fine
mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());
when i deploy the web application in the remote server that have the mysql database and try to use that web application am getting no database error connection !! if i change database connection to :
mysql_connect("localhost","username","password") or die(mysql_error());
using localhost (127.0.0.1) instead of using ip address ever thing work fine , i have tried check /etc/my.conf file and add bind-address=192.168.15.15 and restart mysql server
still cant connect to database
any idea !!!
You need to check two things. First, make sure the port 3306 is open for connections. Second, if you're connecting remotely, you'll need to create a wildcard user (The host will be a percentage sign %). Take a look at the mysql.user table. If the user you're trying to connect to has a Host of localhost, you won't be able to connect remotely.
To create a new user:
CREATE USER 'newuser'#'%' IDENTIFIED BY 'password';
Give that user permissions, I suggest to only the database in question:
GRANT ALL PRIVILEGES ON your_database.* TO 'newuser'#'%';
Finally, reload the privileges:
FLUSH PRIVILEGES;
problem was i was enabling the SElinux , so i enabled httpd network with commad :
setsebool -P httpd_can_network_connect=1

Mysql - connect to remote server using IP address

I am using PHP and MySQL to make a database connection, the only change is that I am trying to access my remote server from my local computer using my server IP address.
mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());
and the error I am getting is Unable to connect to the given host. The user has all privileges and rights.
Most default installs of MySQL only listen to the local machine.
Look for the bind-address setting in my.cnf on the server, and make sure it's listening on the IP address you're targetting. You may also need to ensure skip-networking isn't switched on!
Alternatively (and less securely!) the following will set it up to listen on all addresses - local and remote:
bind-address = 0.0.0.0
Before you try using PHP todo this. Open up your terminal or console and type the following:
$ mysql -u username -h x.xx.xx.x -p
then enter your password
Or on windows then type:
\path\to\mysql.exe -u username -h x.xx.xx.x -p
then enter your password
This will give you a more detailed response as to the authentication issue that's coming up. And you can be 100% sure that your remote login from your IP address works. If it works fine then its something in your PHP code that you're missing.
Try to add a user (USER-NAME) allowed to connect from the IP:
mysql> GRANT ALL PRIVILEGES ON *.* TO USER-NAME#IP IDENTIFIED BY "PASSWORD";
USER-NAME is the username that you would like to create (like 'widor')
IP is the public IP address of your remote connection (like '195.x.y.z')
Of course, limit the privileges you want to grant (ALL PRIVILEGES is probably not your choice)
1) Check which interface MySQL listen for connections, local (localhost, 127.0.0.1) or remote (0.0.0.0 or IP address). It's in my.cnf. http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
2) Check does your user have corresponding privileges. User that logs in from localhost can have different priveleges from remote one. For example 'user'#'localhost' and 'user'#'%'

Categories