Ubuntu中MySQL安装与使用笔记
2013-10-16 00:26
423 查看
mysql安装
$ sudo apt-get install mysql-server mysql-clientmysql服务器启动、关闭和重启
$ sudo /etc/init.d/mysqlstart/stop/restart设置初始密码
$ mysqladmin -u root -p password 你的密码修改密码
$ mysqladmin -u root -p password 你的新密码(终端会提示输入原始密码)
Enter password:
mysql登录本地服务器(例如登录root用户)
$ mysql -uroot -p或者:
Enter password:
$ mysql -hlocalhost -uroot -p
mysql登录远程服务器(例如登录root用户)
$ mysql -h hostname/ip -P portnum -uroot -pEnter password:
注意-P(大写)指定端口号,该参数可以省略,省略后将连接默认端口3306.
创建新用户及设置权限(GRANT命令)
GRANT可以用来创建用户并同时设置权限,也可以对已有用户设置或者修改权限,使用方法相似,GRANT的格式如下:grant <privileges> on 数据库对象(database.table) to 用户(user@host) [IDENTIFIED BY "<password>"] [WITH GRANT OPTION];
(1)如果指定了IDENTIFIED BY "<password>",如果user@host不存在则该命令将创建一个新用户并指定权限<privileges>,如果user@host存在则该命令将指定权限<privileges>,若密码与原始密码不一致,还会修改成新的密码。因此指定了IDENTIFIED BY,GRANT可以创建用户、修改密码、指定权限等;
(2)如果没有指定IDENTIFIED BY,GRANT可以用来指定权限,也就是设置或者修改权限是不用指定密码的;
(3)<privileges>字段可以指定为"ALL PRIVILEGES"表示所有权限,或者其他指定的多个以逗号隔开的权限字段;
(4)database.table字段表示某个数据库的某个表,*.*表示所有的数据库的所有表,dbname.*表示数据库dbname的所有表;
(5)user@host字段表示host主机上的user用户,user@localhost表示user用户只能从本地访问,user@"%"表示user用户可以从任意主机访问,user@'192.168.0.1'表示用户user只能从指定主机访问。
例子:
For creating a new user with all privileges (use only for troubleshooting), at mysql prompt type:
$ mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
For creating a new user with fewer privileges (should work for most web applications) which can only use the database named "database1", at mysql prompt type:
$ mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON database1.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword';
yourusername and yourpassword can be anything you like. database1 is the name of the database the user gets access to. localhost is the location which gets access to your database. You can change it to '%' (or to hostnames or ip addresses) to allow connections
from every location (or only from specific locations) to the database. Note, that this can be a security problem and should only be used for testing purposes!
【关于GRANT的详细用法可以从网上查看相关资料】
查看用户权限
$ mysql> show grants for user@host;$ mysql> show grants for user; 等价于:$ mysql> show grants for user@"%";
启动与停止
1、启动
MySQL安装完成后启动文件mysql在/etc/init.d目录下,在需要启动时运行下面命令即可。
[root@localhost mysql]# start
/etc/init.d/mysql
2、停止
[root@localhost mysql] # /usr/bin/mysqladmin -u root -p shutdown
3、自动启动
1)察看mysql是否在自动启动列表中
[root@localhost mysql] # /sbin/chkconfig –list
2)把MySQL添加到你系统的启动服务组里面去
[root@localhost mysql] # /sbin/chkconfig – add mysql
3)把MySQL从启动服务组里面删除。
[root@localhost mysql] # /sbin/chkconfig – del mysql
相关文章推荐
- Ubuntu中MySQL安装与使用笔记
- Ubuntu中MySQL安装与使用笔记
- ubuntu安装使用笔记06——maven安装配置
- Ubuntu 16.04 安装使用MySQL以及安装mysql-workbench,ubuntu16.04设置禁止mysql开机自启动
- python安装mysql-python简明笔记(ubuntu环境)
- Freebsd中mysql安装及使用笔记
- 解决ubuntu下安装mysql使用service 无法启动问题
- ubuntu14.04 adb的安装和使用笔记
- Java学习笔记——MySQL的安装使用以及SQL语法简介
- Ubuntu16.04使用docker安装MySQL
- 基于Ubuntu+Apache+Passenger+MySQL的Redmine安装笔记
- 在ubuntu下使用源码包安装mysql
- Ubuntu使用Anaconda安装Theano笔记
- Ubuntu 下快速部署安装 Apache + PHP + MySQL + phpMyAdmin 笔记
- Ubuntu 14 下使用离线包安装mysql
- Freebsd中mysql安装及使用笔记
- Ubuntu 安装mysql,以及使用C++开发简单示例
- Ubuntu安装Mysql及使用
- Ubuntu 使用笔记------win7下安装ubuntu12.04(64位)