I want to echo or print content from a database in my terminal on my raspberry pi. I installed MySql client and PHP5. I want to run a php file in my terminal so i created a file:
<?php
$con = mysqli_connect("mysql51-025.wc1","username","password","dbname")
//first one is hostname
if(mysqli_connect_errno()){
echo "failed to connect with error: ". mysqli_connect_error();
}
?>
When i run this in my terminal on my raspberrypi
pi#raspberrypi ~ $ php database.php
I'll get this returned:
PHP warning: mysqli_connect(): (hy000/2005): Unknown MySQL server host
'nameofmyhostinphpfile' (25) in /home/pi/database.php on line 8
failed to connect with error: Unknown MySql server host 'hostname'
My question is how i can make this connection work. And will i be able to execute queries and echo or print those results on my pi and use those results in shell scripts?
Thank you!
Related
I'm running mongodb on my local mac which was running fine in combination with MAMP. However it's not working, and when I call it in terminal, I now get this:
$ mongo
MongoDB shell version: 3.2.11
connecting to: test
2017-02-23T18:43:39.841+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2017-02-23T18:43:39.842+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
exception: connect failed
Any ideas?
Thanks!
mongo is off, you can run mongod to see where the initialization fails
I have install mysql on digital ocean and trying to connect through php script and I got this error
mysqli_connect(): (HY000/2002): Connection refused in
/var/www/waev.in/signup/ajax/send_code.php on line 9 Failed to connect
to MySQL: Connection refused
What can be the issue :
my php script
<?php
include '../func/sms_function.php';
$mysql_host='{ip}';
$mysql_user='root';
$mysql_pass='********';
$my_db='wesearch_waev_user';
$con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Maybe this would be
$mysql_host="localhost";// for local server
Or
$mysql_host="$ip";// if database is in remote and IP is stored in $ip
Check the port you are using on the server. MySQL needs to be running on the port 3306
I am new to MongoDB.I installed mongodb and used with Laravel framework.Its had been worked for a long time without any issues.But currently while i try to acess my website it shows:
Failed to connect to: localhost:27017: Connection refused
when i try to acces mongodb via command line,it shows:
warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146 exception: connect failed
when i try to access it via rock tool,it shows:
Unable to connect MongoDB, please check your configurations. MongoDB said:Failed to connect to: 127.0.0.1:27017: Connection refused.
what i will do? I can't figure what's wrong from my part since its working after the past months.I do nothing since the last days.Thanks in advance..
After a short break, I can find out the solution.
The origin of that issue is the unexpected shutdown of the system.So i run the below code in command prompt:
sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
then after the above code run, its working fine :)
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2003):
Can't connect to MySQL server on '209...' (13) in
/home/www/hookahondemond.com/Sendorder01010101010101.php on line 77
Failed to connect to MySQL: Can't connect to MySQL server on
'209...' (13)
my php code
<?php
$con=mysqli_connect("209.17.116.155","mir****","M****6","stores",'3306');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Login to Apache/PHP server and make sure that you can connect to the MySQL port:
telnet <mysql host> <mysql port>
If you cannot telnet the port, then it could be either one of these:
a network (firewall? routing?) problem
your MySQL server is currently not running
your MySQL host/port is incorrect
your MySQL is running but does not accept tcp connections (it has --skip-networking option set)
If you can telnet the MySQL host/port, then there is no network/MySQL setting problem,
and actually, you should be seeing a different error message (like credential error)
I am really a beginner in PHP and Mysql. I made a database on static IP 192.168.1.211 which is based on CentOS(only Command prompt) and on this IP there is no other software like easyPHP, and I am working on static IP 192.168.1.20 based on Windows 7. I also installed easyPHP and Dreamweaver... using Dreamweaver I made one .php file and I tried to use the database which is on 192.168.1.20..
using below code
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect(server2,'root','password','vvani');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but I get an error as shown below
Notice: Use of undefined constant server2 - assumed 'server2' in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Failed to connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known.
How can I access the database on 192.168.1.211 from 192.168.1.20 in PHP code?
I also tried hard to find a solution from Google, but I am not getting a perfect solution.
Just read errors and correct it..
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect($server2,'root','password','vvani'); // here, $server2 is variable, not constant
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Change server2 to $server2. If that still doesn't work then it's possible the other server does not allow requests in such a way so you will have to update the MySQL settings on that server to allow external access to the port (or otherwise forward it).
Thanks Elon Than and Explosion Pills,
I changed server2 to $server2, but I get new warning
Warning: mysqli_connect(): (HY000/1130): Host '192.168.1.20' is not allowed to connect to this MySQL server in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
And I research on it and I try to give remotely access to database on another server.
Follow some few steps and solved problem.
# mysql -u root -p
mysql> GRANT ALL ON Database.* TO <un>#'localhost' IDENTIFIED BY 'password';
Thanks again.