简体   繁体   中英

How can I connect to a MariaDB (MySQL) instance on localhost?

I am installing FrontAccounting with MariaDB (rather than MySQL) just for my own personal use, accessing it via browser on http://localhost/frontaccounts . But attempts to log in as root to set up the database don't work: Cannot connect to database. User or password is invalid ... . Similar error messages occur when trying to log on as root in phpMyAdmin, SQLbuddy, MySQL Workbench, etc.

(In fact, this problem can also occur with MySQL, so you can substitute 'MySQL' wherever 'MariaDB' appears in what follows.)

The problem is that I am trying to log on as root from my computer's IP address ( root@MYCOMPUTER ), externally visible to the internet, to a server running on a "different" IP address (127.0.0.1, localhost) which requires root@localhost -- even though it is the same computer!

How to fix this?

The following answer that I eventually found will allow you to create a user at one IP address to access the MariaDB (MySQL) server at another address, even if that address is 127.0.0.1 or localhost .

In a terminal window, type:

$ sudo mysql -u root -p -h localhost

Type your password:

[sudo] password for YOURUSERNAME: 

Type the MariaDB root password, or press Enter (for default blank password):

Enter password: 

The MariaDB Monitor welcome banner appears ...

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 39
Server version: 10.0.28-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

... and the monitor command prompt waits for your input ( none means you are not connected to any database yet):

MariaDB [(none)]> 

Create a user at localhost that MariaDB will accept, and quit:

MariaDB [(none)]> CREATE USER 'root'@'%';
     -> \q;
Bye
~$

This allows you to at least connect, and is a model for allowing other users to connect. These minimal instructions leave security holes so, after you have created users on MariaDB, given them passwords (don't forget root ), and granted privileges and roles, these holes will be closed.

For additional security, specify the IP address of the users' machines, eg 'root'@'localhost' instead of 'root'@'%' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM