0

I am new to MySQL , i am trying to make a simple registration form using MySQL database and python with FLask as severside framework .

I got the following error when running code in localhost : enter image description here

MySQL configuration :

app.config['MySQL_HOST'] ='localhost'
app.config['MySQL_USER'] = 'blacksmith'
app.config['MySQL_PASSWORD'] = '123456'
app.config['MySQL_DB'] = 'myflaskapp'
app.config['MySQL_CURSORCLASS'] = 'DictCursor'

register function with route

@app.route('/register',methods=['GET','POST']) #route accepts get request per default but we precise here post also
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST' and form.validate(): # check if form is validated and its a post request
        name = form.name.data
        email = form.email.data
        username= form.username.data
        password = sha256_crypt.encrypt(str(form.password.data))

        #create cursor
        cur =mysql.connection.cursor()

        # execute SQL command
        cur.execute("INSERT INTO users(name,email,username,password) VALUES (%s,%s,%s,%s)",(name,email,username,password) )

        #commit to DB
        mysql.connection.commit()

        #close connection
        cur.close()

    return render_template('register.htm',form=form)

I got the same error ( Access denied for user root@localhost ) also when i access MySQL shell in my bash shell using the command

mysql -u root

( i had to use sudo command to access the database )

The problem that i didn't set any password for MySQL database access , and the same error persists even when trying to change it with another password using the command :

ALTER USER 'user'@'hostname' IDENTIFIED BY 'newPass';

I tried also to change password in MySQL configuration with no success .

I am stuck with that any help please !

3
  • Hi :) Have you tried to do it? B.4.3.2.3 Resetting the Root Password: Generic Instructions Commented May 18, 2020 at 0:42
  • check the grants for that user Commented May 18, 2020 at 0:57
  • @Bukse thanks but that didn't help : Operation ALTER USER failed for 'root@localhost' Commented May 18, 2020 at 0:57

2 Answers 2

0

I solved that issue by running mysql server in safe mode :

sudo mysqld_safe --skip-grant-tables
service mysql stop
mysqld_safe --skip-grant-tables &

if you encounter ( mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists. ) error

 mkdir -p /var/run/mysqld
 chown -R mysql:mysql /var/run/mysqld
  • the server works now in safe mode , open a new bash command and execute

    mysql -u root

that should work normally

Sign up to request clarification or add additional context in comments.

Comments

-1

"Using password: NO". So wherever you are creating a connection, you aren't using a password.

4 Comments

Then why is the access denied , what's wrong with that ?
you have MySQL_PASSWORD set in your configuraiton. The error message shows its not being used. Look at where mysql.connection is made and see if it actually uses the password.
@danblack Dude, don't do that, Just don't.
hmm. No "do" instructions where issued, just look at how it is. Answer could have been better, but probably too later now. Considering the other answer of run without any authentication barriers ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.