I’m a newbie in Raspberry Pi and MySQL.
I’ve created a code, database in phpmyadmin which ran by wamp server in my PC. Meanwhile, I want to run this code in Raspberry Pi 3. Besides that, when I run the code in Raspberry Pi 3, I want the data can be save in phpmyadmin, thus, when I run localhost on my PC, the data that I enter from Raspberry Pi can be saved in the PC too. But, when I ran it, it shows an error message like this:
mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied
for user ‘root’@’localhost’
Here is my db:
#connect to db
db = mysql.connector.connect(host = 'localhost', user = 'root', password =
'', database = 'user')
Anyone know how to solve this problem?
4
Answers
Use below query and execute it on your database.
I have a Raspberry Pi and did the following steps:
Update the operating system:
sudo apt-get update && sudo apt-get upgrade
Install mySQL server:
sudo apt-get install mysql-server
Install mySQL client:
sudo apt-get install mysql-client
Check status of mySQL:
sudo service mysql status
Install mySQL python connector:
sudo apt-get -y install python3-mysql.connector
Give mySQL a password:
sudo mysql -uroot
use mysql;
update user set authentication_string=PASSWORD(‘mynewpassword’) where User=’root’;
Note: In the above command keep the root user, just set the password.
Exit mySQL:
q
Stop mySQL services:
sudo /etc/init.d/mysql stop
Start mySQL services:
sudo /etc/init.d/mysql start
Test the new password by logging in to the database:
sudo mysql -u root -p
SHOW DATABASES;
CREATE DATABASE YOUR_DATABASE_NAME_HERE;
CREATE USER ‘YOUR_NEW_USERNAME’@’localhost’ IDENTIFIED BY ‘YOUR_NEW_PASSWORD’;
Note: Python needs a new user to allow communication to mySQL! I have no idea why but it works.
In Python:
It took me several days to piece this together from a dozen different websites. Hopefully my notes didn’t miss anything. Creating the new user at the end seems to be the key issue. Mine isn’t a Windows installation but the installation should be similar.
You should only create a user in the database(mysql) through which you are accessing the database.
Password must satisfy the requirement(1.One Upper case,2.One special character).
Now use python
Now add privilege to the user
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root' @ 'localhost' (using password: NO)
but I was trying to use python scripts like CGI.
this worked for me:
This is part of my code
/usr/lib/cgi-bin/script_cgi.py
:you have to give it permissions:
sudo chmod 755 /usr/lib/cgi-bin/script_cgi.py
You must also enable the apache module:
sudo a2enmod cgid
then visit localhost and Apache Server can now use Python scripts as CGI:
http://127.0.0.1/cgi-bin/script_cgi.py
For more of CGI whith python in https://www.tutorialspoint.com/python/python_cgi_programming.htm