首页 > 代码库 > Ubuntu安装MySQL及设置字符集为UTF-8

Ubuntu安装MySQL及设置字符集为UTF-8

一、安装数据库

1、获取并安装mysql

$ sudo apt-get install mysql-server

2、设置root密码(在跳出的提示框中输入密码即可)

3、登陆数据库,输入mysql -u root -p,接着出现输入密码的提示,输入完密码回车即可

$ mysql -u root -pEnter passwordWelcome to the MySQL monitor.

4、查看数据库,输入show databases;

mysql > show databases;

5、显示当前已有的数据库

+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema |+--------------------+

二、设置字符集

1、打开mysql配置文件

$ sudo vim /etc/mysql/my.cnf

2、找到[mysqld]在下方输入下方代码后,点击Esc并输入:wq保存退出

[mysqld]init_connect=SET collation_connection = utf8_unicode_ciinit_connect=SET NAMES utf8character-set-server=utf8collation-server=utf8_unicode_ciskip-character-set-client-handshake

3、重启mysql服务

$ service mysql restart

4、输入mysql -u root -p 重新进入mysql

$ mysql -u root -pEnter password

5、输入show variables like ‘%character%‘ 查看字符集

mysql> show variables like %character%;+--------------------------+----------------------------+| Variable_name            | Value                      |+--------------------------+----------------------------+| character_set_client     | utf8                       || character_set_connection | utf8                       || character_set_database   | utf8                       || character_set_filesystem | binary                     || character_set_results    | utf8                       || character_set_server     | utf8                       || character_set_system     | utf8                       || character_sets_dir       | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+

可以看到,已经成功将字符集更改为utf-8格式

 

Ubuntu安装MySQL及设置字符集为UTF-8