最近在学习R语言,看到R与数据库交互这一部分,就自己动手实践了一下,数据库选择的是mysql,主要记录下linux下怎么安装mysql。
网上的很多资料都有相关的文章,这里只是记录下自己安装过程中遇到的问题和解决方法:
linux内核版本:
~$ uname -a
Linux naci 3.19.0-16-generic #16-Ubuntu SMP Thu Apr 30 16:09:58 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
1.首先在liunx下安装Mysql数据库
~$ sudo apt-get install mysql-server #安装Mysql服务器端
~$ ps -aux|grep mysql #检查Mysql服务器进程
beili 10301 0.0 0.0 37080 3604 pts/7 S+ 5月20 0:00 mysql -u root -p
mysql 25125 0.0 0.0 4472 1704 ? Ss 09:51 0:00 /bin/sh /usr/bin/mysqld_safe
mysql 25496 0.0 3.1 859956 127084 ? Sl 09:51 0:01 /usr/sbin/mysqd --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
beili 26652 0.0 0.0 13708 2180 pts/19 S+ 10:32 0:00 grep --color=auto mysql
~$ netstat -nlt|grep 3306 #检查Mysql服务器占用端口
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
~$ /etc/init.d/mysql status # 通过启动命令检查Mysql服务器状态
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2015-05-21 09:51:13 CST; 43min ago
Process: 25126 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 25122 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
~$ service mysql status # 通过系统服务检查Mysql服务器状态
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2015-05-21 09:51:13 CST; 44min ago
Process: 25126 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 25122 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
~$ whereis mysql # 查看mysql各个文件安装的目录
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
2.访问和配置Mysql
$ mysql -u root -p
Enter password:
mysql默认的字符集为latin1的,所以要改为utf8的。很多网上的文章执行“sudo vi /etc/mysql/my.cnf”,可是打开一看,里面就两行话:
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
想来mysql的配置文件应该是在那两个文件夹下面,于是尝试之下打开了/etc/mysql/mysql.conf.d/ 下的mysqld.cnf文件,执行下面的命令:
~$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
...
[client] default-character-set=utf8
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
character-set-server=utf8 collation-server=utf8_general_ci
...
注:棕色部分是添加的内容
重启mysql服务,并查看字符集的设置情况:
~$ sudo /etc/init.d/mysql restart
~$ mysql -u root -p
mysql> show variables like "%char%"
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+