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.
Related
I am a beginner with PHP and with mySQL. I am trying to do something very simple. I have read through many solutions on Stack Overflow and I have tried many different things but nothing has worked. I am simply trying to connect to MySQL with PHP. I have a MySQL Active instance running. I am using a Mac. I can successfully connect to it using the following commands from the terminal:
mysql -u root -p
This then prompts me to enter my password:
Enter password:
I then type in my password. It is a throwaway password. It is "Snow1234". It then works successfully. It shows
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
....
mysql>
I can then enter commands in my terminal which all work correctly.
I am trying to do the same thing with php (keep in mind that I am a beginner and I have never used it before).
I have a file called form.php.
form.php:
<?php
mysql_connect('localhost','root','Snow1234');
?>
I save the file. Then, I upload the file to a URL that I own using Filezilla. Then, I visit the URL in my browser (Google Chrome):
http://www.myWebsite.com/form.php
However, it says on the website:
ERROR: Could not connect. Access denied for user 'root'#'localhost'
(using password: YES)
How do I fix this?
I see you have uploaded form.php to a server. You have to check The password and username to the database hosted at your server.
ERROR: Could not connect. Access denied for user 'root'#'localhost'
(using password: YES)
This error is simply wrong password or username to connect to the database. You could connect to the database on your terminal, probably because you are connecting to your local database(your computer). But your form.php is now on the server attempting to connect to the database there (on the server).
You will need to access the CPANEL of your server to help you create the database etc.
Not sure if there is a need for you to have this on the server(ie accessible from anywhere on the internet). Otherwise, consider installingXampp to your computer. Then,
Have the form.php stored in a folder {myproject} C://XAMPP/htdocs (depending on where you install it).
Create a database through localhost/phpmyadmin
Connect to the DB at your PHP side, the same way as you do now.
But note, by default the username is root and password is empty.
You can now access your form via http://localhost/myproject/form.php
Final Note: this is only accessible on your computer.
Please refer this link will help you.
1.Open a Connection to MySQL
2.MySQLi Procedural
3.PDO Connection
[https://www.w3schools.com/php/php_mysql_connect.asp]
I'm new to Ubuntu. I tried to run a .php file and connect it to a database. Everything is on-set. I already imported the database in phpMyAdmin but every time I access my database,it returns an error
This page isn’t working localhost is currently unable to handle this
request. HTTP ERROR 500
Turns out, it seems like my database isn't running at all. In Windows I just open the XAMPP and click Apache and MySQL buttons. While in Ubuntu,
I have no idea on how to start or run MySQL and Apache. I already tried running commands on the terminal but it won't help. Someone has already installed it on this computer, I just don't how to run it and what web-server platform is this running.
How do I do it and how would I know that my database is running and accessible?
Try to connect your database and access database via command line.
mysql -u [username] -p
you can replace [username] with your real username of mysql like root
it will prompt for password so you are type yours like root
prompt will say
mysql>
now you need to list all databases to see is database exists or not
show databases;
it will list down all databases. you may verify is your exists or not
then you can select database by
use databasename;
and then run
show tables;
it will show all tables.
so you can verify that mysql working, database exists and tables are there or not.
Use this command it will start the database is you have it
systemctl mysql start
This should do the trick you need to have mysql database or maria db installed
Check by running this command in terminal after the first one
mysql
And you can also add argument like host and login
mysql -h (your host default is localhost) -u (user default is root) -p (password default is none)
Check your files access level
sudo chmod -R 777 "location of your file"
Good day!
I have a simple php code that will export a database
exec("E:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump --user=root --password= --host=localhost accounts > E:\database_backup\backup.sql");
This code works but I'm accessing the application on another computer locally via ip like (192.168.1.32/filename/backup.php) so it's not saving.
How can I make it work locally? Thank you in advance.
You need to specify database IP in your mysqldump command and you also need to define required grant privileges in DB ( root#anothercomputerIP)
I'm having trouble with a database export that is too big for phpmyadmin to handle.
I'm curious what the syntax would be for exporting a mysql database from a remote server and getting the export file into a specific folder on my own computer, all using the command line.
Say the database name is: mydb
Say the remote server's ip is: 123.456.789.101
Say my username is: tic
Say my password is: toc
Say the folder i want the mysql dump transferred into is my downloads folder, and say I am on a windows 7 pc.
unix/linux command solutions are welcome. Bonus points for solutions that work on microsoft.
You can run
mysqldump -h yourhostname -u youruser -p yourdatabasename > C:\your\file\path.sql
-h connects you to the remote servers IP so you can dump the data locally, as long as your user has the correct privileges and you can connect remotely to your database server.
However, you may need to ssh into your server first. If you are running windows, Download putty and connect to your host from this tool.
Once on remote server execute: >mysqldump -u youruser -p yourdbname > /your/file/path.sql
After that, download a ftp client tool like winscp or filezilla and transfer (copy) the .sql file from your remote host onto your local host.
You can read more here and here.
I have a shellscript with connects to a a different machine with ssh and a key so it does not need the username and password.
When i run this script from commandline it works fine.. but when I run this script from php shell_exec it does not work.
If I make an ssh connection with PHP and run the script as my own user it does work.
Now for my question :D
Is there a way to just running the script in shell_exec from php without making an connection over ssh as a different user?
Did you specify the private key file correctly?
If you are using Ubuntu or Debian the web server is running with the user name www-data. For other systems please check the web server configuration for the user name. You can simply test if this user (and your php web application) is able to do the SSH connection.
1) Become the user of your web server
sudo su www-data
2) Try connecting the remote host
ssh remoteUser#remoteHost
If you will get connected without entering a password there must be a different problem. If you have to enter a password, the key files were stored for a different user - not for www-data. You have already configured SSH to use the key. Do the same for your local user www-data and it will work.
It seems ssh connection does not work with shell_exec. If i run the shellscript under ssh2_exec it does seem to work.
Which is a little strange as the ssh connection is made in the script file with a public and private key.. I would assume this would just run :s
The webserver is allowed to execute the file, as there are other command in there who work as expected.