时间:2021-07-01 10:21:17 帮助过:22人阅读
Ubuntu中MySQL安装与使用笔记
mysql安装
$ sudo apt-get install mysql-server mysql-client
mysql服务器启动、关闭和重启
$ sudo /etc/init.d/mysql start/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 -p
Enter password:
注意-P(大写)指定端口号,该参数可以省略,省略后将连接默认端口3306.
创建新用户及设置权限(GRANT命令)
GRANT可以用来创建用户并同时设置权限,也可以对已有用户设置或者修改权限,使用方法相似,GRANT的格式如下:
grant
(1)如果指定了IDENTIFIED BY "
(2)如果没有指定IDENTIFIED BY,GRANT可以用来指定权限,也就是设置或者修改权限是不用指定密码的;
(3)
(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@"%";
bitsCN.com