---
title: "Ubuntu下MySQL的安装及远程连接配置等配置"
author: "jerry051635"
date: "2011-11-07T10:09:48Z"
tags: []
categories: ["数据库"]
slug: "ubuntu-mysql"
draft: false
lastmod: "2011-11-07T10:10:33Z"
id: 1237204
url: "https://jerry051635.iteye.com/blog/1237204"
---

一、ubuntu下MySQL的安装

在ubuntu命令行下输入 sudo apt-get install mysql-server 即可安装mysql服务，默认在安装mysql-server时，会自动安装好mysql-client。
同时安装好的mysql配置文件位置：/etc/mysql/my.cnf
启动mysql的快捷图标为：/etc/init.d/mysql

二、编码问题的解决

    查看当前MYSQL字符集：show variables like 'character%';

    更改编码：sudo vim /etc/mysql/my.cnf

    找到 添加如下两句:
    //默认字符集为utf8
    default-character-set=utf8

    找到 添加添加如下四句:(注意每一行的前后都不能有空格)
    //默认字符集为utf8
    default-character-set=utf8 
    //设定连接mysql数据库时使用utf8编码，以让mysql数据库为utf8运行
    init_connect='SET NAMES utf8'

    最后切换到vim的末行模式下执行wq退出vim编辑器.
 
    停止数据库服务：sudo /etc/init.d/mysql stop 
    重新启动：         s udo /etc/init.d/mysql start 
    进入数据库：      mysql -uroot -p(你的密码) 
    查看编码：         show variables like '% character %';
    此时应该就全是 utf8编码了。

三、 MySQL服务 加入开机自启动
     加入开机自启动:        sudo update-rc.d mysql defaults
     从开机自启动中移出:    sudo update-rc.d -f mysql remove


四、远程连接ubuntu下MySQL
     vim /etc/mysql/my.cnf找到
     bind-address = 127.0.0.1
     这行，注释掉（如下）
     #bind-address = 127.0.0.1
     或者改为
     <span style="color:red">bind-address = 0.0.0.0</span>
     允许任意IP访问，或者自己指定一个IP地址。

     然后重启 MySQL

     sudo /etc/init.d/mysql restart

     授权用户能进行远程连接
<span style="color:red">     grant all privileges on *.* to root@"%" identified by "password" with grant option;
      flush privileges;
</span>
   第一行命令解释如下，*.*：第一个*代表数据库名；第二个*代表表名。这里的意思是所有数据库里的所有表都授权给用户。root：授予root账号。“%”：表示授权的用户IP可以指定，这里代表任意的IP地址都能访问MySQL数据库。“password”：分配账号对应的密码，这里密码自己替换成你的mysql root帐号密码。

   第二行命令是刷新权限信息，也即是让我们所作的设置马上生效。

   此时再远程连接ubuntu下的MySQL应该能够连接上了