I'm new to Python and Django.
I'm configuring a Django project using a PostgreSQL database engine backend, But I'm getting errors on each database operation. For example when I run manage.py syncdb, I'm getting:
C:\xampp\htdocs\djangodir>python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
67, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
__import__(name)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py"
, line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_
signal
File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 6, in
<module>
from django.db import models
File "C:\Python27\lib\site-packages\django\db\__init__.py", line 77, in <modul
e>
connection = connections[DEFAULT_DB_ALIAS]
File "C:\Python27\lib\site-packages\django\db\utils.py", line 92, in __getitem
__
backend = load_backend(db['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\utils.py", line 33, in load_back
end
return import_module('.base', backend_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
__import__(name)
File "C:\Python27\lib\site-packages\django\db\backends\postgresql\base.py", li
ne 23, in <module>
raise ImproperlyConfigured("Error loading psycopg module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No mo
dule named psycopg
Can someone give me a clue on what is going on?
You need to install psycopg2 Python library.
Installation
Download http://initd.org/psycopg/, then install it under Python PATH
After downloading, easily extract the tarball and:
$ python setup.py install
Or if you wish, install it by either easy_install or pip.
(I prefer to use pip over easy_install for no reason.)
$ easy_install psycopg2
$ pip install psycopg2
Configuration
in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_user_password',
'HOST': '',
'PORT': 'db_port_number',
}
}
- Other installation instructions can be found at download page and install page.
Also make sure you have the PostgreSQL development package installed.
On Ubuntu you need to do something like this:
$ sudo apt-get install libpq-dev
Step by step that I use:
- sudo apt-get install python-dev
- sudo apt-get install postgresql-server-dev-9.1
- sudo apt-get install python-psycopg2 - Or sudo pip install psycopg2
You may want to install a graphic tool to manage your databases, for that you can do:
sudo apt-get install postgresql pgadmin4
After, you must change Postgre user password, then do:
- sudo su
- su postgres -c psql postgres
- ALTER USER postgres WITH PASSWORD 'YourPassWordHere';
- \q
On your settings.py file you do:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
}
}
Extra:
If you want to create the db using the command line you can just do:
- sudo su
- su postgres -c psql postgres
- CREATE DATABASE dbname;
- CREATE USER djangouser WITH ENCRYPTED PASSWORD 'myPasswordHere';
- GRANT ALL PRIVILEGES ON DATABASE dbname TO djangouser;
On your settings.py file you do:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'djangouser',
'PASSWORD': 'myPasswordHere',
'HOST': '',
'PORT': '',
}
}
You can install "psycopg" with the following command:
# sudo easy_install psycopg2
Alternatively, you can use pip :
# pip install psycopg2
easy_install and pip are included with ActivePython, or manually installed from the respective project sites.
Or, simply get the pre-built Windows installer.
This may seem a bit lengthy, but it worked for me without any error.
At first, Install phppgadmin from Ubuntu Software Center.
Then run these steps in terminal.
sudo apt-get install libpq-dev python-dev
pip install psycopg2
sudo apt-get install postgresql postgresql-contrib phppgadmin
Start the apache server
sudo service apache2 start
Now run this too in terminal, to edit the apache file.
sudo gedit /etc/apache2/apache2.conf
Add the following line to the opened file:
Include /etc/apache2/conf.d/phppgadmin
Now reload apache. Use terminal.
sudo /etc/init.d/apache2 reload
Now you will have to create a new database. Login as 'postgres' user. Continue in terminal.
sudo su - postgres
In case you have trouble with the password of 'postgres', you can change it using the answer here https://stackoverflow.com/a/12721020/1990793 and continue with the steps.
Now create a database
createdb <db_name>
Now create a new user to login to phppgadmin later, providing a new password.
createuser -P <new_user>
Now your postgressql has been setup, and you can go to:
http://localhost/phppgadmin/
and login using the new user you've created, in order to view the database.
The immediate problem seems to be that you're missing the psycopg2 module.
If you are using Fedora 20, Django 1.6.5, postgresql 9.3.* and you need the psycopg2 module, do this:
yum install postgresql-devel
easy_install psycopg2
If you are like me, you may have trouble finding the well documented libpq-dev rpm... The above worked for me just now.
I was having the same Issue on Mac.
The solution was to use only PIP to install everything, and touch some things.
First install PIP from: https://pip.pypa.io/en/latest/
Then you want to make sure if path to pg_config is in your PATH (echo $PATH), if not you can edit your bash_profile:
vi /Users/<user>/.bash_profile
and add this line:
export PATH=$PATH:/path/to/pg_config/bin
If you don't know where pg_config is you can use the "locate" tool, but be sure your locate.db is up to date (i was using an old locate.db and using paths that does not exists).
sudo /usr/libexec/locate.updatedb
locate pg_config
Then install Django (if needed) and psycopg2.
sudo pip install Django
sudo pip install psycopg2
And then in settings.py (localhost:defaultport)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
}
}
Greets!
$ sudo apt-get install libpq-dev
Year, this solve my problem.
After execute this, do:
pip install psycopg2
Steps to create a Django app with PostgreSQL database from scratch.
Check if PostgreSQL is installed in your system. In your bash shell enter
psql --version
If PostgreSQL is installed jump to step 6.
To install PostgreSQL -
sudo apt-get install python3-dev libpq-dev
sudo apt-get install postgresql postgresql-contrib
By default PostgreSQL installation creates a special user named "postgres" that has all rights. To login to PostgreSQL
sudo -u postgres psql
To exit PostgreSQL session
\q
Now set password for "postgres" user.
Login to PostgreSQL -
sudo -u postgres psql
In PostgreSQL interactive session enter -
\password postgres
Create PostgreSQL database.
To create directly from bash shell -
sudo -u postgres createdb your_db_name
To create from PostgreSQL interactive session -
createdb your_db_name
To check if the database is created, list all databases using PostgreSQL command
\l
If you already have an existing Django project then jump to step 11. Otherwise create a virtual environment. I use virtualenv wrapper.
mkvirtualenv your_project_name
Create Django project.
django-admin startproject your_project_name
Cd to the project directory and create your app.
python manage.py startapp your_app_name
Install Django.
pip install django
Install Psycopg2. Psycopg2 is a PostgreSQL database adapter for Python. This is required for Django to connect with PostgreSQL.
pip install psycopg2
Update settings.py as follows
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your_db_name',
'USER': 'postgres',
'PASSWORD': 'xxxxx',
'HOST': 'localhost',
'PORT': '',
}
}
Run migration.
python3 manage.py migrate
Run Django server and visit localhost:8000
python3 manage.py runserver 0.0.0.0:8000
If required you can install pgadmin - a graphical client to view and manipulate PostgreSQL database schema and data.
Please note that installation of psycopg2 via pip or setup.py requires to have Visual Studio 2008 (more precisely executable file vcvarsall.bat). If you don't have admin rights to install it or set the appropriate PATH variable on Windows, you can download already compiled library from here.
This is one of the very good and step by step process to set up PostgreSQL in ubuntu server. I have tried it with Ubuntu 16.04 and its working.
https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04
integrating postgresql in django
Arch: Ubuntu and Nginx
sudo apt install libpq-dev postgresql postgresql-contrib
systemctl status postgresql.service is usually active by now if not check back installation
By default, Postgres uses an authentication scheme called “peer authentication” for local connections. Basically, this means that if the user’s operating system username john#example matches a valid Postgres username john, that user can login with no further authentication. During the Postgres installation, an operating system user named postgres was created to correspond to the postgres administrative user. We use this user to perform administrative tasks.
sudo -u postgres psql
postgres=# create database example;
CREATE DATABASE
postgres=# create user john with password 'abcxyz';
CREATE ROLE
postgres=# ALTER ROLE john SET client_encoding TO 'utf8';
postgres=# ALTER ROLE john SET default_transaction_isolation TO 'read committed';
postgres=# ALTER ROLE john SET timezone TO 'UTC';
postgres=# GRANT ALL PRIVILEGES ON DATABASE example TO john;
Postgres is now set up so that Django can connect to and manage its database information.
pip install psycopg2-binary is not recommended for production as psycopg2 has unfixed issues.
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'example',
'USER': 'john',
'PASSWORD': 'abcxyz',
'HOST': 'localhost',
'PORT': '5432', can be left empty
}
}
At the end, use these basic django migration commands to complete the integration.
python manage.py makemigrations
python manage.py migrate
Extra TODO Tips
It is highly recommended to use better methods to set critical values like NAME, USER and PASSWORD rather than using plain text like shown above in database code snippet.
One method could be using a json file to replace those contents from outside the scope of project root directories. So even in unforeseeable cases of attempted source code hijacking your critical auth details could be out of reach.
A new config.json file with corresponding key-value pairs is assumed to be created.
{
"POSTGRES_DB": "example",
"POSTGRES_USER": "john",
"POSTGRES_PASSWORD": "abcxyz",
"POSTGRES_HOST": "localhost",
"POSTGRES_PORT": ""
}
Placing this code in settings.py to load and use config.json.
import os
import json
with open('/path to your config.json file/') as config_file:
config = json.load(config_file)
Update database config to load the params from config.json file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config['POSTGRES_DB'],
'USER': config['POSTGRES_USER'],
'PASSWORD': config['POSTGRES_PASSWORD'],
'HOST': config['POSTGRES_HOST'],
'PORT': config['POSTGRES_PORT'],
}
}
postgres runs on port 5432
specify this in settings.py
https://www.postgresql.org/docs/14/app-postgres.html
Related
import MySQLDB as Database
File "c:\pythonprojects\env\lib\site-packages\mysql_python-1.2.2-py3.6-win32.egg\mysqldb__init__py", line 22
raise ImportError, "This is MySQL Version %s, But _mysql is version %r"
SyntaxError: invalid syntax
Make sure you have pymysql installed. Then in settings.py do this
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
i) First you have to install the mysql server in your machine, if you already have mysql server up and running then jump to next step.
ii) Create a user in mysql (you can use the root user, but I would suggest create a separate one) you can refer mysql documentation for creating a new user. Let, you have created a user with:- username = testuser , password = testpass#123
iii) Create a database in mysql , let named it testDB
iv) Now you have to install mysqlclient-python, to install it from PyPI you can do so by typing pip install mysqlclient (by default pip the python package manager points python2 series if you are setting up your django project on python3 then you have to use pip3) for more information on how to install mysqlclient-python on your particular OS you can refer this link: https://github.com/PyMySQL/mysqlclient-python
v) Now In settings.py under key DATABASES make these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # for mysql engine
'NAME': 'testDB',
'USER': 'testuser',
'PASSWORD': 'testpass#123',
'HOST': 'localhost', # if you want to run your project on your localmachine
}
}
vi) Now change your working directory to your django project directory: " cd yourDjangoProject" and run these django commands
python manage.py makemigrations
python manage.py migrate
Now I hope that above django commands will run successfully and you would be able to use to your mysql database in your django project.
I am am working on setting up Django Project for running tests. But I am getting below error:
Got an error creating the test database: permission denied to copy database "template_postgis"
Note: My default application's database is working fine. The issue is happening while running tests.
Complete stack trace is as:
moin#moin-pc:~/workspace/mozio$ python manage.py test --verbosity=3
nosetests --verbosity=3
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
Creating test database for alias 'default' ('test_my_db')...
Got an error creating the test database: permission denied to copy database "template_postgis"
Type 'yes' if you would like to try deleting the test database 'test_mozio_db_test', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: must be owner of database test_mozio_db_test
Below is the DATABASE configuration of setting.py:
POSTGIS_VERSION = (2, 2, 2)
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_pass',
'HOST': '<HOST_IP',
'PORT': '',
'TEST': {
'NAME': 'test_my_db',
},
}
}
Any help regarding this? Below are the steps I tried:
Grant create DB access to user:
ALTER USER my_user CREATEDB;
Grant all privileges to user for test_my_db database:
GRANT ALL PRIVILEGES ON DATABASE test_mozio_db_test to mozio;
Edit:
After fixing above issue, I was also getting error as:
django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5: "polygon" geometry(POLYGON,4326) NOT NULL,
Updated my answer to fix both the issues.
I finally found how to fix this issue. The problem is that when I created template_postgis, I didn’t set it to be a template.
You can check it via doing:
SELECT * FROM pg_database;
You can fix it by setting datistemplate=true for template_postgis by running:
update pg_database set datistemplate=true where datname='template_postgis';
After that in case you are getting error related to geometry like:
django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5: "polygon" geometry(POLYGON,4326) NOT NULL,
That is because you need to add extension postgix with database. In order to fix this, add postgis to template_postgis like:
psql -d psql -d template_postgis -c 'create extension postgis;'
Note: You must be superuser to create this extension.
Install packages first correctly.
sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib
During installation postgres user is created automatically.
sudo su - postgres
You should now be in a shell session for the postgres user. Log into a Postgres session by typing:
psql
CREATE DATABASE myproject;
In your settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
I am trying to setup continuous integration for my django project. Since I am using postgresql als my database, I want to setup the same in travis. This is my travis.yml file:
language: python
services:
- postgresql
python:
- 3.4
env:
- DJANGO=1.8 DB=postgres
before_install:
- export DJANGO_SETTINGS_MODULE=ledenbestand.settings.local
install:
- pip install -r travis/requirements.txt
before_script:
- psql -c 'create database ledenbestand;' -U postgres
script:
- python manage.py test
notifications:
email:
recipients:
- random#email.com
on_success: never
on_failure: always
The problem is that this will fail because my local.py settings also gives a password when connecting to the database, the postgres user on travis doesn't have a password:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ledenbestand',
'USER': 'postgres',
**'PASSWORD': 'password',**
'HOST': 'localhost',
'PORT': '5432',
}
}
The travis documentation says the following:
Should your local test setup use different credentials or settings to access the local test database, we recommend putting these settings in a database.yml.travis in your repository and copying that over as part of your build:
before_script:
- cp config/database.yml.travis config/database.yml
I however have no clue how this could work, how can this line of code overwrite my local.py settings?
Fixed this by adding an If statement in my config file:
if 'TRAVIS' in os.environ:`
So I'm following this tutorial http://rosslaird.com/blog/building-a-project-with-mezzanine/ for building a project with Mezzanine. I am extremely new to all of this stuff (including Linux and the command line) and frankly do not really know what I am doing. I am at this part of the tutorial:
Run this command within the same directory as your local_settings.py and settings.py files: python manage.py createdb
The tutorial says that after I enter the "python manage" command I will be "asked to create a super-user, to provide details that user, and to answer a few more questions". When I entered the command none of those questions showed up. Why is this? Thank you very much in advance.
So you are trying to run this on the commandline right (terminal)?
sudo -u postgres createuser --superuser $USER
sudo -u postgres psql
postgres=# \password [enter your username]
Enter new password:
Enter it again:
\q
createdb $USER;
Change $USER with your designated name.
Sorry that the link you provided is 404'd (Most likely because this post is over 2 years old) .
But... I think I found it here (sort of)... There's a few bits and pieces missing which might have made it confusing. The "manage.py" should be in the parent directory that's prior to where your "settings.py | local_settings.py | urls.py" files reside. Just make sure you are in the appropriate directory when running the manage command. An easy ls or ls -la command will show you where your files are at within the directory. I myself am a novice Mezzanine user. I've been playing around with it for a year now and hope this info can serve as a quickstart guide for setting up Mezzanine on PostgreSQL while also resolving your issue.
So... Once the following conditions are met you should be able to create a Mezzanine project with a PostgreSQL database instance. But first, make sure you have Mezzanine set up and running without warnings or errors.
For Mezzanine Setup...
Preconditions:
You've installed Python, pip, etc...
Get virtualenv & virtualenvwrapper installed and configured.
pip install virtualenv
pip install virtualenvwrapper
Make sure to add your environment variables for your virtual environments. Just point the paths to where you want your Mezzanine projects to live as well as the environments. An easy way to do it is edit your bashrc in the home directory. I like to keep my virtualenvs separate from my working directory but adjust the paths to how you like. Just sudo vi ~/.bashrc then add the following...
## Virtualenvwrapper Settings
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/envs/mezzanine/projects/live/here
source /usr/local/bin/virtualenvwrapper.sh
Note: Do i to insert text and when done editing type ctrl + c to exit the prompt then :wq! to write (save) and quit. Then close your terminal and open a new one so the new changes take effect. If not, restart your computer.
Next, cd over to your project directory cd ~/envs/mezzanine/projects/live/here and create your virtualenv for your Mezzanine project (It'll activate once created).
mkvirtualenv environment_name
You can deactivate your environment by simply typing deactivate in terminal. To re-activate your environment, type workon environment_name
Now you can install Mezzanine...
pip install -U mezzanine
Then create your Mezzanine project and watch those folders get created in your project directory...
mezzanine-project project_name
Collect your templates & static files.
python manage.py collecttemplates
python manage.py collectstatic
Now create your db instance (by default this will be SQLite if you haven't changed anything in settings.py .
Make sure you have your ALLOWED_HOSTS configured and edit your settings.py if you haven't already.
vi ~/envs/mezzanine/projects/live/here/project_name/project_name/settings.py .
ALLOWED_HOSTS = [
'127.0.0.1:8000',
'localhost',
'www.mydomain.com' #if you want to set that too.
]
Note: Don't forget to save your changes ctrl+c & :wq! .
At this point you should be able to go back a directory and run your server python manage.py runserver and get a response from your localhost (loopback address) at port 8000 by opening a browser and typing in 127.0.0.1:8000. (make sure your environment has been activated first)
Now For Your PostgreSQL Database...
Check this out. It's a pretty good resource and even touches base on virtualenv. You can also replace the Django references with Mezzanine (almost). The most important part is the database setup portion...
https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04
Install Postgres and dependencies. (You might need to run as sudo with -H flag here)
sudo apt-get install libpq-dev python-dev postgresql postgresql-contrib
Install psysopg2 (Might need sudo -H as well)
sudo pip install psycopg2
Login as "postgres" user:
sudo -su postgres
Run the psql shell command: psql. You should see the 'postgres=#' text.
Now, create your database (Remember to end psql statements with a semicolon;) CREATE DATABASE mydb;
Create database user: CREATE USER mydbuser WITH PASSWORD 'mypassword';
Set your user roles:
ALTER ROLE mydbuser SET client_encoding TO 'utf8';
ALTER ROLE mydbuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE mydbuser SET timezone TO 'UTC';
8: Then give the database user access rights: GRANT ALL PRIVILEGES ON DATABASE mydb TO mydbuser;
Type ctrl + d to exit shell, then type exit to exit postgres user.
Now, go to your settings.py and local_settings.py in your Mezzanine project's working directory and modify your DATABASES settings with the credentials you previously created... cd ~/envs/mezzanine/projects/live/here/project_name/project_name/ and then vi settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'mydbuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Note: Don't forget local_settings.py
Now you can create your database via manage.py.
python manage.py createdb
The above command should prompt you to do the initial setup of your database along with your site info, as well as create a superuser. Just follow the prompts. To create additional superusers just do
`python manage.py createsuperuser' .
Now go back a directory to the project root cd .. and run your server python manage.py runserver . And now... you should have your new Mezzanine project running on PostgreSQL. Congratulations!! :)
The tutorial is just wrong. The writer has got confused with the Postgres createdb command used earlier, and the actual manage.py command, which is syncdb.
You would be better off using the actual Django tutorial.
when i try to view the databases in mysql i get this error:
ERROR 1018 (HY000): Can't read dir of '.' (errno: 13)
And that stops my app from displaying...
My django debugger says:
(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/my_database' (13)")
Here is my settings file :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'my_database', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '****', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
What can cause the problem?
Thanks in advance
You need to set ownership and permissions for directory:
chown -R mysql:mysql /var/lib/mysql/ #your mysql user may have different name
chmod -R 755 /var/lib/mysql/
Note: -R makes commands recursive - you may omit it, if there is no subdirs in /var/lib/mysql/.
This should work for Mac users:
sudo chown -R mysql:mysql /usr/local/mysql/
sudo chmod -R 755 /usr/local/mysql/
If this doesn't work, try running which mysql to see where your mysql installation is located, and then replace /usr/local/mysql/ in the command above with whatever is before the 'bin' directory.
For example, on my system which mysql produces the following output:
/usr/local/mysql/bin/mysql
so my path is /usr/local/mysql/
On CentOS/RedHat, you should do the same thing on a different path:
chown -R mysql:mysql /data/mysql/
chmod -R 755 /data/mysql/
chown -R mysql:mysql /var/lib/mysql/
chmod -R 755 /var/lib/mysql/
I can confirm that these two chmod statements worked for me (Webmin didn't see the databases nor did show tables) but I'm not sure why I had to do this after setting up perhaps two dozen servers (Centos) with MySQL in that past few years.
osx high sierra use the following command solves the issue:
chown -R mysql:mysql /usr/local/mysql
if you installed mariadb using homebrew you can run the following the command for OS X
sudo chown -R mysql:mysql /var/lib/var/mysql/
sudo chmod -R 777 /usr/local/var/mysql/