选择语言 :

 Core_Controller_RunTime::mysql_get_servers

获取需要检测的mysql服务器列表

返回的内容类似

array
(
    '127.0.0.1:3307' => array
    (
        'database' => 'test',
        'username' => 'root',
        'password' => '123456,
    ),
    '127.0.0.1:3308' => array
    (
        'database' => 'test',
        'username' => 'root',
        'password' => '123456,
    ),
)
array Core_Controller_RunTime::mysql_get_servers( )
返回值
  • array
File: ./core/controllers-shell/runtime.controller.php
protected function mysql_get_servers()
{
    // 获取所有database的配置
    $all_config = Core::config('database');

    // runtime数据库相关设置
    /*
        // 例如:
        $runtime_config = array
        (
            //相关设置,比如可以设定不同的用户名和密码和库
            'servers' => array
            (
                '127.0.0.1:3306' => array('username'=>'root','password'=>'123456'),
            ),
            'ignore'  => array('127.0.0.1:3306'),        //忽略的服务器
        );
    */
    $runtime_config = Core::config('core.server_runtime.database');

    $mysql_servers = array();
    foreach ($all_config as $name => $config)
    {
        if ($config['connection'])
        {

            // 解析connection,比如 mysql::test:123@127.0.0.1:3360/test
            if ( !is_array($config['connection']) )
            {
                $config['connection'] = Database::parse_dsn($config['connection']);
            }

            // 端口
            $port = $config['port'] ? $config['port'] : 3306;

            if ( is_array($config['connection']['hostname']) )
            {
                foreach ($config['connection']['hostname'] as $hostname)
                {
                    if ( is_array($hostname) )
                    {
                        foreach ($hostname as $s)
                        {
                            $mysql_servers[$s.':'.$port] = array
                            (
                                'database'     => $config['database'],
                                'username'     => $config['username'],
                                'password'     => $config['password'],
                            );
                        }
                    }
                }
            }
        }
    }

    // 处理忽略不检查的数据库
    if ( isset($runtime_config['ignore']) && $runtime_config['ignore'] )
    {
        if ( is_array($runtime_config['ignore']) )
        {
            foreach ($runtime_config['ignore'] as $ignore_server)
            {
                if (false===strpos($ignore_server,':'))$ignore_server .= ':3306';
                unset($mysql_servers[$ignore_server]);
            }
        }
        else
        {
            if (false===strpos($runtime_config['ignore'],':'))$runtime_config['ignore'] .= ':3306';
            unset($mysql_servers[$runtime_config['ignore']]);
        }
    }

    if ( isset($runtime_config['servers']) && is_array($runtime_config['servers']) && $runtime_config['servers'] )foreach ($runtime_config['servers'] as $s=>$c)
    {
        if (!is_array($c))
        {
            $this->output('runtime servers config error,key='.$s.'. this value need an array like :'."array('username'=>'root','password'=>'123456')");
            continue;
        }

        if (false===strpos($s,':'))$s .= ':3306';

        if ( isset($mysql_servers[$s]) ) $mysql_servers[$s] = array_merge($mysql_servers[$s],$c);
    }

    $mysql_servers = array
    (
        '127.0.0.1:3306' => array('username'=>'root','password'=>123456),
        '127.0.0.1:3309' => array('username'=>'root','password'=>123456),
    );

    if (!$mysql_servers)
    {
        $this->output('no server.');
    }
    else
    {
        $this->output('server list:');
        print_r($mysql_servers);
    }

    return $mysql_servers;
}