skip to Main Content

i can’t log in to mysql via pdo when i try to execute connection to db via apache but on cli it works just fine

in symfony 4.1 controller i have inserted this string

new PDO('mysql:host=db;dbname=test_db', 'xyz', 'uzzzxxxx');

and it give Exception but on Symfony4.1 command the same line works and i can query db.
What can cause this? Controller and command are in one bundle

3

Answers


  1. Chosen as BEST ANSWER

    In my case at fault was SELinux by disabling it, now it works in case someone want to check there is tutorial https://linuxize.com/post/how-to-disable-selinux-on-centos-7/


  2. You have to give Apache enable writing to log files and file uploads as well? Let’s play nice with SELinux. And set the proper ownership and permissions

    # Ownership
    

    sudo chown apache:apache -R /data/www/html/sites/mysite
    cd /data/www/html/sites/mysite

    File permissions, recursive

    find . -type f -exec chmod 0644 {} ;

    Dir permissions, recursive

    find . -type d -exec chmod 0755 {} ;

    SELinux serve files off Apache, resursive

    sudo chcon -t httpd_sys_content_t /data/www/html/sites/mysite -R

    Allow write only to specific dirs

    sudo chcon -t httpd_sys_rw_content_t /data/www/html/sites/mysite/logs -R
    sudo chcon -t httpd_sys_rw_content_t /data/www/html/sites/mysite/uploads -R

    httpd_sys_content_t – for allowing Apache to serve these contents and httpd_sys_rw_content_t – for allowing Apache to write to those path.

    That’s it! I enjoyed and you share!

    Login or Signup to reply.
  3. I believe for Apache you are looking for localhost as your host.

     new PDO('mysql:host=localhost;dbname=test_db', 'xyz', 'uzzzxxxx');
    

    While I’m unfamiliar with Symfony, it may be that either it uses different credentials or it may be that when it detects an error, it sets it to localhost, whereas Apache does not.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search