最近打算做点东西,后面会自己想一个业务需求表,自己设计流程图,废话不多说了,进入我们的mysq之旅:
安装、配置mysql
(1)使用Ubuntu Software Center:打开Ubuntu Software Center,在右上角的搜索框查询mysql,然后选定MySQL Server,点击安装即可。
同时安装好的mysql配置文件位置:/etc/mysql/my.cnf
启动mysql的快捷图标为:/etc/init.d/mysql
(2)使用apt:打开终端执行 ”sudo apt-get install mysql-server“ 即可。
MySQL完成安装后可以直接使用root账户登录,且该账户默认是没有密码的。注意这里的root角色就是指你的Ubuntu的root角色,如果你当前使用的系统帐号不是root的话,也不必切换到系统root账户,可以在登录MySQL的时候使用“-u"这个参数来指定登录账户。如:
$ mysql -u root
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> select Host, User from user;
+-----------+------------------+
| Host | User |
+-----------+------------------+
| 127.0.0.1 | root |
| ::1 | root |
| iUbuntu | |
| iUbuntu | root |
| localhost | |
| localhost | debian-sys-maint |
| localhost | root |
+-----------+------------------+
7 rows in set (0.00 sec)
因为此时root账户默认没有密码,所以不用输入密码就能以root角色登录并查看所有信息的权限。如果换成非root角色登录MySQL,则只拥有部分数据库操作权限。
$ mysql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> use mysql
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
因此MySQL完成安装后的第一件事就是给root用户设置密码,否则数据库将毫无安全可言。
mysql> GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "";
将以上命令中的替换为你要设定的密码,以上命令的意思是对在本机(localhost)使用密码登录的root用户赋予所有数据库的操作权限。设置密码后,如果再以root用户登录就需要输入密码了,如:
$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.5.34-0ubuntu0.13.10.1 (Ubuntu)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
建立数据库独立用户
因为root用户拥有数据库的所有操作权限,所以不能轻易地提供给别人使用。在一个MySQL实例中可以创建多个数据库,这些数据库可能归属于不同项目,每个数据库的操作角色也不一样。对此可以针对不同那个数据库指定用户进行访问。
首先使用root角色创建一个数据库
mysql> create database db_web_monitor
然后将这个数据库授予一个叫xavier的用户使用
mysql> GRANT ALL PRIVILEGES ON db_web_monitor.* TO xavier@localhost IDENTIFIED BY "xavier";
这样就可以使用xavier用户,密码为xavier在本机登录MySQL操作db_web_monitor数据库了。
$ mysql -u xavier
ERROR 1045 (28000): Access denied for user 'xavier'@'localhost' (using password: NO)
$ mysql -u xavier -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 77
Server version: 5.5.34-0ubuntu0.13.10.1 (Ubuntu)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_web_monitor |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>
开放远程登录权限
1. 首先修改MySQL的配置文件,允许监听远程登录。
$ sudo vi /etc/mysql/my.cnf
找到bind-address所在行
45 # Instead of skip-networking the default is now to listen only on
46 # localhost which is more compatible and is not less secure.
47 bind-address = 127.0.0.1
将 bind-address值修改为本机IP即可。注意:此处的IP只是支持本地访问的,所以如果想任意主机访问的话,需要将其注释掉
2. 授予用户远程登录权限。
mysql>GRANT ALL PRIVILEGES ON db_web_monitor.* TO xavier@"%" IDENTIFIED BY "xavier";
如此这般,xavier用户就可以在任意主机通过IP访问到本机MySQL,对db_web_monitor数据库进行操作了。
一、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
重新启动: sudo /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帐号密码。
第二行命令是刷新权限信息,也即是让我们所作的设置马上生效。