ubuntu下MySQL一些配置

一、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字符集[在mysql命令行模式下执行]:show variables like 'character%';

    更改编码:sudo vim /etc/mysql/my.cnf

    找到[client] 添加如下两句:
    //默认字符集为utf8
    default-character-set=utf8

    找到[mysqld] 添加添加如下四句:(注意每一行的前后都不能有空格)
    //默认字符集为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
     或者改为
     bind-address = 0.0.0.0
     允许任意IP访问,或者自己指定一个IP地址。

     然后重启 MySQL

     sudo /etc/init.d/mysql restart

 

   授权用户能进行远程连接

   grant all privileges on *.* to root@"%" identified by "password" with grant option;

 

   flush privileges;

   第一行命令解释如下,*.*:第一个*代表数据库名;第二个*代表表名。这里的意思是所有数据库里的所有表都授权给用户。root:授予root账号。 “%”:表示授权的用户IP可以指定,这里代表任意的IP地址都能访问MySQL数据库。“password”:分配账号对应的密码,这里密码自己替换成 你的mysql root帐号密码。

   第二行命令是刷新权限信息,也即是让我们所作的设置马上生效。

 

   此时再远程连接ubuntu下的MySQL应该能够连接上了

 

来自:http://blog.chinaunix.net/uid-22954746-id-2418833.html

 

 

#
# The MySQL database server configuration file.
#
[client]
port        = 3306
socket        = /var/run/mysqld/mysqld.sock
default-character-set   = utf8

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket        = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#My Features
default-character-set   = utf8
skip-name-resolve
default-storage-engine = InnoDB
default-table-type = InnoDB
innodb_flush_log_at_trx_commit=0
federated

#Master
server-id = 1
log-bin=mysql-bin
binlog-do-db=s3
binlog-ignore-db=mysql
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1

#Slave
skip-name-resolve
slave-skip-errors 
log-slave-updates
master-user=sync
master-password=123456
master-port=3306
master-connect-retry=30
replicate-do-db=s3
relay-log=S3-DB-M-1-relay-bin
#
# * Basic Settings
#

user        = mysql
socket        = /var/run/mysqld/mysqld.sock
port        = 3306
basedir        = /usr
datadir        = /var/lib/mysql
tmpdir        = /tmp
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        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer        = 16M
max_allowed_packet  = 512M
max_heap_table_size = 800M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
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        = 16M
#
# * 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

log_error                = /var/log/mysql/error.log

# Here you can see queries with especially long duration
#log_slow_queries    = /var/log/mysql/mysql-slow.log
#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

[mysqldump]
quick
quote-names
max_allowed_packet    = 24M

[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/

 

如果你已经安装完毕并且运行过emma,程序就会创建 ~/.emma/emmarc文件,保存你自己的一些配置。

vim ~/.emma/emmarc   

找到
db_encoding=latin1  
改为
db_encoding=utf8  

然后里,在执行所有的sql语句之前加入这条sql语句
set names utf8  
按ctrl+enter执行之后,再看看是不是已经ok了。正常显示中文了
自动的执行“set names utf8” 语句。
ubuntu的apt-get 安装emma是在/usr/share/emma目录下面。
   cd /usr/share/emma/emmalib  
   sudo vim __init__.py  
找到
   "db_encoding": "latin1"  
改为
   "db_encoding": "utf8"  
保存退出。以后新创建的配置文件默认就会是utf8的解码了,我想在连接数据之后就执行 “set names utf8” 语句,所以
    sudo vim /usr/share/emma/emmalib/mysql_host.py  
跳到155行左右的_use_db(self, name, do_query=True)函数哪里,改成如下
def _use_db(self, name, do_query=True):  
        if self.current_db and name == self.current_db.name: return  
        if do_query:   
                self.query("use `%s`" % name, False)  
                self.query("set names utf8",  False)  
        try:  
                self.current_db = self.databases[name]  
        except KeyError:  
                print "Warning: used an unknown database %r! please refresh host!\n%s" % (name, "".join(traceback.format_stack()))  
自己看着改,反正改成上面的样子,注意空格,因为python是以空格当语句的分割语法的。
其实就是加了一句
self.query("set names utf8",  False)  
我机器上的就打不开然后:
self.query("set names utf8",  False)
self.query("use `%s`" % name, False) 
换了一下位子就ok了。

 

 

 

你可能感兴趣的:(ubuntu)