1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

[Resolved] Mysqli

Discussion in 'Development' started by Whaxion, Feb 27, 2017.

  1. Whaxion

    Whaxion Creeper

    Messages:
    5
    GitHub:
    whaxion
    Hello !
    I want to connect to my database, but that doesn't work... :'(
    With the same code on my webserver, that works, but with PocketMine-MP, that said an error
    Code:
    Warning: mysqli::__construct(): (HY000/2002): No such file or directory
    Here is my code:
    PHP:
    <?php
    namespace be\minesylv\Database;

    use 
    pocketmine\plugin\PluginBase;

    class 
    Main extends PluginBase {
        private 
    $db;
     
        public function 
    onLoad(){
            
    $this->saveDefaultConfig();
            
    $host $this->getConfig()->get('host');
            
    $user $this->getConfig()->get('user');
            
    $pswd $this->getConfig()->get('password');
            
    $database $this->getConfig()->get('database');
            if(
    $host == "127.0.0.1"){
                
    $host "localhost";
            }
            
    $this->db = new \mysqli('localhost','user','password','database_name');
            
    //$this->db = new \mysqli($host,$user,$pswd,$database);
            
    if(!empty($this->db->connect_errno)){
             
    $this->getLogger()->error('Can\'t connect to database : ' .$this->db->connect_errno ' | ' .$this->db->connect_error);
            } else {
             
    $this->getLogger()->info('Successfully connected to the Database');
            }
            
    $this->getLogger()->info(php_ini_loaded_file());
        }
        public function 
    getDB(){
         return 
    $this->db;
        }
    }
    Thank you in advance for your answer !
    Whaxion
     
  2. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Use 127.0.0.1 instead of localhost when connecting.
     
    Whaxion likes this.
  3. Whaxion

    Whaxion Creeper

    Messages:
    5
    GitHub:
    whaxion
    Thanks for your answer, but with 127.0.0.1 I have another error
    Code:
    Warning: mysqli::__construct(): (HY000/2002): Connection refused
     
  4. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    This means that there is no MYSQL server running on port 3306 on your local machine. Try restarting it or make sure you have your port correct.
     
    Whaxion likes this.
  5. Whaxion

    Whaxion Creeper

    Messages:
    5
    GitHub:
    whaxion
    The server is started and on port 3306...
    I already try to restart it but that didn't change anything :(
     
  6. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Did you already do mysql_secure_installation? (If your on linux ubuntu) Otherwise I recommend taking a look at your MYSQL configuration
     
    Whaxion likes this.
  7. Whaxion

    Whaxion Creeper

    Messages:
    5
    GitHub:
    whaxion
    I don't know what is mysql_secure_installation... :/
    My server is on Debian 8 and this is my mysql configuration
    Code:
    #
    # The MySQL database server configuration file.
    #
    # You can copy this to one of:
    # - "/etc/mysql/my.cnf" to set global options,
    # - "~/.my.cnf" to set user-specific options.
    # 
    # One can use all long options that the program supports.
    # Run program with --help to get a list of available options and with
    # --print-defaults to see which it would actually understand and use.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
    
    # This will be passed to all mysql clients
    # It has been reported that passwords should be enclosed with ticks/quotes
    # escpecially if they contain "#" chars...
    # Remember to edit /etc/mysql/debian.cnf when changing the socket location.
    [client]
    port        = 3306
    socket        = /var/run/mysqld/mysqld.sock
    
    # Here is entries for some specific programs
    # The following values assume you have at least 32M ram
    
    # This was formally known as [safe_mysqld]. Both versions are currently parsed.
    [mysqld_safe]
    socket        = /var/run/mysqld/mysqld.sock
    nice        = 0
    
    [mysqld]
    #
    # * Basic Settings
    #
    user        = mysql
    pid-file    = /var/run/mysqld/mysqld.pid
    socket        = /var/run/mysqld/mysqld.sock
    port        = 3306
    basedir        = /usr
    datadir        = /var/lib/mysql
    tmpdir        = /tmp
    lc-messages-dir    = /usr/share/mysql
    skip-external-locking
    #
    # Instead of skip-networking the default is now to listen only on
    # localhost which is more compatible and is not less secure.
    bind-address        = 188.165.160.96
    #
    # * Fine Tuning
    #
    key_buffer        = 16M
    max_allowed_packet    = 16M
    thread_stack        = 192K
    thread_cache_size       = 8
    # This replaces the startup script and checks MyISAM tables if needed
    # the first time they are touched
    myisam-recover         = BACKUP
    #max_connections        = 100
    #table_cache            = 64
    #thread_concurrency     = 10
    #
    # * Query Cache Configuration
    #
    query_cache_limit    = 1M
    query_cache_size        = 512M
    #
    # * Logging and Replication
    #
    # Both location gets rotated by the cronjob.
    # Be aware that this log type is a performance killer.
    # As of 5.1 you can enable the log at runtime!
    #general_log_file        = /var/log/mysql/mysql.log
    #general_log             = 1
    #
    # Error log - should be very few entries.
    #
    log_error = /var/log/mysql/error.log
    #
    # Here you can see queries with especially long duration
    #slow_query_log_file = /var/log/mysql/mysql-slow.log
    #slow_query_log      = 1
    #long_query_time = 2
    #log_queries_not_using_indexes
    #
    # The following can be used as easy to replay backup logs or for replication.
    # note: if you are setting up a replication slave, see README.Debian about
    #       other settings you may need to change.
    #server-id        = 1
    #log_bin            = /var/log/mysql/mysql-bin.log
    expire_logs_days    = 10
    max_binlog_size         = 100M
    #binlog_do_db        = include_database_name
    #binlog_ignore_db    = include_database_name
    #
    # * InnoDB
    #
    # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
    # Read the manual for more InnoDB related options. There are many!
    #
    # * Security Features
    #
    # Read the manual, too, if you want chroot!
    # chroot = /var/lib/mysql/
    #
    # For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
    #
    # ssl-ca=/etc/mysql/cacert.pem
    # ssl-cert=/etc/mysql/server-cert.pem
    # ssl-key=/etc/mysql/server-key.pem
    secure-file-priv = ""
    
    
    [mysqldump]
    quick
    quote-names
    max_allowed_packet    = 16M
    
    [mysql]
    #no-auto-rehash    # faster start of mysql but no tab completition
    
    [isamchk]
    key_buffer        = 16M
    
    #
    # * IMPORTANT: Additional settings that can override those from this file!
    #   The files must end with '.cnf', otherwise they'll be ignored.
    #
    !includedir /etc/mysql/conf.d/
    
    Thanks you to try to help me :)
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Better comment out the bind address part.
    Code:
    #bind-address        = 188.165.160.96
    
     
    Whaxion and LilCrispy2o9 like this.
  9. Whaxion

    Whaxion Creeper

    Messages:
    5
    GitHub:
    whaxion
    That's resolve my problem !
    Thanks ! :)
     
    Muqsit and LilCrispy2o9 like this.
  10. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Glad you fixed it :)
     
    Kyd, Muqsit and Whaxion like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.