c9.io is a verygood website
I have a php zoon , and when I want to connect to MySQL,I don't know password.
I have try [space] root ...
but,all is wrong.
I can open mysql in shell , no password ,my operate return error:
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
It's looks like, mysqld is not started 。(I have try “mysqld start”,but faild)
What I want to know,
Is Mysql service free on c9.io ?
We are extremely happy to announce our first iteration of MySQL support in Cloud9. It makes it super easy to install, start and stop a MySQL instance right in your workspace. The nice thing is that every workspace will run a separate database so your projects will never interfere with each other. You can control MySQL with the mysql-ctl command line tool run from the terminal.
# start MySQL. Will create an empty database on first start
$ mysql-ctl start
# stop MySQL
$ mysql-ctl stop
# run the MySQL interactive shell
$ mysql-ctl cli
You can then connect to the database with following parameters:
Option Value Comment
Hostname $IP The same local IP as the application you run on Cloud9
Port 3306 The default MySQL port number
User $C9_USER Your Cloud9 user name
Password - No password since you can only access the DB from within the workspace
Database c9 The database name
Of course this is just the beginning. We have for example plans to add a management UI to start and stop databases or pre-installing tools like phpMyAdmin. However we don’t want to make you wait for the fully integrated feature while we already have something that is enabling a lot of use cases and still super easy to use.
Stay tuned and happy coding.
The documentation show how start, stop, and run the mysql environment.
Start the MySQL shell mysql-ctl start then in yor file.php:
$ip = getenv("REMOTE_ADDR");
$port = "3306";
$user = "YorUsername";
$DB = "c9";
$conn = mysql_connect('$ip', '$user', '', '$db', '$port')or die(mysql_error());
mysql_select_db('$db','$conn')or die(mysql_error());
mysql_query("select * from YourTableName",'$conn')or die(mysql_error());
The line getenv("REMOTE_ADDR") return the same local IP as the application you run on Cloud9.
Related
I would like to host a database on my raspberry pi to which I can access from any device. I would like to access the contents of the database using python.
What I've done so far:
I installed the necessary mysql packages, including apache 2.
I created my first database which I named test.
I wrote a simple php
script that connects and displays all the contents of my simple
database. The script is located on the raspberry pi at /var/www/html
and is executed when I enter the following from my laptop
(192.168.3.14/select.php)
Now my goal is to be able to connect to the database using python from my laptop. But I seem to have an error connecting to it, this is what I wrote to connect to it.
db = MySQLdb.connect("192.168.3.14","root","12345","test" )
Any help or direction is appreciated.
on the terminal of your raspi use the following command:
mysql -u -p -h --port
where you switch out your hostname with your ip address. since currently you can only connect via local host
at first step is check you haven't firewall rules on raspberry or in your lattop
after you can try this command on mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%';
and remember to apply new privileges
FLUSH PRIVILEGES;
for more detail you can see
https://dev.mysql.com/doc/refman/5.7/en/grant.html
is similar for mariadb ecc...
The reason why you are not able to connect to the database from outside of localhost is that the remote access for the root user is prohibited by default, i.e. you can only access the database with the root user from localhost. You can, however, change this by tweaking root's privileges. Please take a look here to find out how the user privileges need to be change to make this work.
If have an amazon ec2 instance running php 5.5 and apache2. I am connecting to my RDS database using a simple test.php script. When I run the script as ec2-user or as root I am able to get a connection.
When I run the same script via my webserver I get
Connection Can't connect to MySQL server on 'ip' (13).
I've tried the public and private IP addresses. This script does work from my other, non-amazon, webservers running the same php and apache. Not sure what to look at next since it works on the command line. Here's the example of the test.php script, pretty straight forward.
define("DB_IP”,”ip”);
define("DB_USERNAME”,”username”);
define("DB_PASSWORD”,”password”);
define("DB_DATABASE”,”database”);
echo "Connecting...\n";
$conn = new mysqli(DB_IP, DB_USERNAME, DB_PASSWORD, DB_DATABASE, 3306);
// Check connection
if ($conn->connect_error) {
die(date('H:i:s')." Connection " . $conn->connect_error);
}
echo date('H:i:s')." Connected successfully\n";
Why would the connection work on the command line but not work when called from the web server?
What are you running on your EC2?
Just asking in case it's an SELinux (Security-Enhanced Linux), in which case it could possibly be the security limitation.
At your terminal, if you run:
getsebool -a | grep httpd
you should be able to see this limitation (whether your webserver can "network" or not. If it cannot, then run this, which should fix your problem:
setsebool -P httpd_can_network_connect 1
Hope that solves it, otherwise I don't see where the issue can arise, especially since you say you can connect via terminal (so AWS security groups should not be the issue).
Keep us posted ;)
How can I connect PHP to an H2 database, so far I tried starting the embedded server with a
$ java -cp h2-1.3.172.jar org.h2.tools.Server
Web Console server running at http://127.0.1.1:8082 (others can connect)
TCP server running at tcp://127.0.1.1:9092 (only local connections)
PG server running at pg://127.0.1.1:5435 (only local connections)
installing the pg-sql module under ubuntu
sudo apt-get install php5-pgsql
then in my php I have the following
$conn = pg_connect("host=127.0.1.1 port=5435 dbname=/home/frank/testdb user=sa password=");
Also tried different ports: 5435, 9092, different host: localhost, 127.0.0.1 but nothing, the returned connection is null or the script hangs.
Any suggestion?
pg_connect is used to connect to PostrgeSQL database server, not for H2DB. Those are completely different products and are not compatibile
To connect to H2 you can use php-java bridge and some custom java class to expose required functionalities to php client
I had it working last year while executing the PHP code in a Java servlet. My experiment is available at https://github.com/webdevelopersdiary/jamp . It's backed by Quercus, and redirects PHP database connection attempts to H2 (at least it does for MySQL, haven't tested for PSQL, but it may do the same in that case). Also note that H2's "compatibility mode" is very rudimentary, it still breaks on pretty standard PSQL stuff that you feed it.
I'm moving a site that had access to phpmyadmin to one where I don't (not yet anyway). Is there a php script to import the generated .sql file into a database? The db is created and ready, just need to import the tables and records.
Try this.
Upload your SQL file to the web space via FTP and execute a page with this code in it.
<?php
$file="path/to/file.sql";
$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file";
exec($command);
?>
Don't forget to set the variables for database name, username, and password. Also, make sure PHP has access to execute commands using the exec function.
Why use php, why not use MySQL itself:
http://dev.mysql.com/doc/refman/5.5/en/mysql.html
Use SSH. Install PuTTy first. Ask your host the server IP, username and password for SSH server, and then do the work. Anyway, how do you think you are going to properly manage your tables and databases without phpmyadmin or any other alternative SQL client, eh? Ask your hosts to install them. Btw, looks like your host's at Antarctica or some other ancient place. I mean, come on man, a SQL client like phpMyAdmin is offered even in free subhosting.
I'd like to establish an ssh tunnel over ssh to my mysql server.
Ideally I'd return a mysqli db pointer just like I was connecting directly.
I'm on a shared host that doesn't have the SSH2 libraries but I might be able to get them installed locally using PECL.
If there's a way that uses native commands that would be great.
I was thinking something like this, but without those libraries it won't work.
$connection = ssh2_connect('SERVER IP', 22);
ssh2_auth_password($connection, 'username', 'password');
$tunnel = ssh2_tunnel($connection, 'DESTINATION IP', 3307);
$db = new mysqli_connect('127.0.0.1', 'DB_USERNAME', 'DB_PASSWORD',
'dbname', 3307, $tunnel)
or die ('Fail: ' . mysql_error());
Anyone have any ideas? I'm running a shared CentOS linux host at liquidweb.
Any thoughts on making the tunnel persistent? Is it possible to establish it with another script and just take advantage of it in PHP?
Thanks.
I would use the autossh tool to create a persistent ssh tunnel to your mysql database. Then all you have to do in your PHP code is point it to localhost.
You can test this (without automatic restart) by doing:
ssh -L 3306:localhost:3306 user#domain.com
Setup a ssh key so that you don't need to use passwords, and tweak the .ssh/authorized_keys file on the mysql system to only allow it to be used for port forwarding.
For more info on ssh tricks see Brian Hatch's excellent series on SSH and port forwarding.
The tunnel must be keep open during the course of the SQL action(s). The following example from RJMetrics explains:
Here's the generic SSH command syntax:
ssh -f -L bind-ip-address:bind-port:remote-ip-address:remote-port \
username#remote-server [command] >> /path/to/logfile
Here's how to securely establish a remote database connection in just 2 lines of PHP code:
shell_exec("ssh -f -L 127.0.0.1:3307:127.0.0.1:3306 user#remote.rjmetrics.com sleep 60 >> logfile");
$db = mysqli_connect("127.0.0.1", "sqluser", "sqlpassword", "rjmadmin", 3307);
We use the shell_exec() function to create the tunnel with a 60 second opening window, and then use the mysqli_connect() function to open a database connection using the forwarded port. Note that we must use the "mysqli" library here because mysql_connect() does not allow us to specify a port and mysql_* functions are deprecated.
sleep 60: When it comes to tunnel connections, we basically have two options: leave the connection open all the time or open it and close it as needed. We prefer the latter, and as such we don't specify the -N option when establishing a tunnel, which would leave it open until the process is manually killed (bad for automation). Since -N is not specified, our tunnel will close itself as soon as its SSH session isn't being used for anything. This is ideal behavior, except for the few seconds between when we create the tunnel and when we get a MySQL connection up and running via the tunnel. To buy us some time during this period, we issue the harmless sleep 60 command when the tunnel is created, which basically buys us 60 seconds to get something else going through the tunnel before it closes itself. As long as a MySQL connection is established in that timeframe, we are all set.
I tried it by doing SSH both by root credentials and and public private key pair. It allows me to conect through command line but not through PHP code.
I also tried by creating a tunnel (by using SSH2 functions), and running shell commands from PHP code (system, exec, etc.); nothing worked.
Finally I tried SSH2 function to execute shell command and it finally worked :)
Here is my code, if it helps you:
$connection = ssh2_connect($remotehost, '22');
if (ssh2_auth_password($connection, $user,$pass)) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
$stream=ssh2_exec($connection,'echo "select * from zingaya.users where id=\"1606\";" | mysql');
stream_set_blocking($stream, true);
while($line = fgets($stream)) {
flush();
echo $line."\n";
}
Try this if want to use PHP functions specifically.
It is possible, but why? It's more complicated than it needs to be, and error prone.
Can you not run the database locally? If not, can you not use something like SQLite, XML files or something else that doesn't require a separate server daemon?
You really do not want to initialise the tunnel inside the PHP scripts. Initialising an SSH tunnel takes a long time (can easily be a second or two), so that will mean every page that connects to the database will have a 2 seconds delay while loading..
If you have to do this (which I strongly recommend against), the best method would be to have a script that maintains the connection in the background..
Setup a SSH keypair. Then using autossh, or a simple script which would execute the required SSH command, wait till the process died and start it again. It could be more intelligent and try and run a command every 10-20 seconds, reconnecting if it fails.
Using the SSH keypair, you wouldn't have to hardcode the remote users password in the script. I would recommend limiting what the remote user can do (by using a dedicated tunnel user, and giving it a restricted shell, see this question)
In short, I would strongly suggest trying SQLite first, then see how feasible it to store data using XML files, then try bugging your web-host about getting a local MySQL server, then look into the SSH tunnelling option.
Eric,
I think you are out of luck on this one. You can either use the ssh extension in your PHP code, or if you have access to the server, you could try to create a ssh tunnel on the command-line.
You probably need special permissions to do that, though. It also looks like you don't have ssh access to this hosting account.
--Joao
I think your best bet is to find a different hosting provider; I suggest a VPS solution (Virtual Private Server) which gives you full control over your web host. THat way, if you don't like the default configuration, you can upgrade it yourself.