日期:2014-05-16  浏览次数:20837 次

linux下mysql的用户管理及访问问题

1.mysql的安装配置

        linux下有一个很神奇的东西叫yum,只要有源,用yum来安装是一件非常容易的事,什么都不用管,它会为你解决好一些软件依赖的问题。一键安装mysql:

[root@localhost ~]# yum install mysql-server mysql-devel

        安装完成后我们就可以使用mysql了:

[root@localhost ~]# /etc/init.d/mysqld start
Starting mysqld:                                           [  OK  ]
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

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> create database db_test;
Query OK, 1 row affected (0.02 sec)

mysql> grant all on *.* to "user"@"localhost" identified by "123";
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

        上面的操作都成功了,包括了登录到mysql,新建数据库和新建一个用户,其实在这个过程中我也遇到了几个问题,花了一些时间,这个后面会说到。先看看mysql的主要配置文件信息:

[root@localhost ~]# cat /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

       datadir=/var/lib/mysql是mysql数据库的数据库文件存放位置,我刚才新建的数据库db_test也在这个目录下。log-error=/var/log/mysqld.log是数据库输出的日志,数据库日志也是在/var/log目录下。mysql默认的监听端口是3306。

2.mysql的用户管理

        安装好mysql后,mysql会自动提供一个空密码的root用户,这个root不是linux的root,而空密码的root用户可以这样登录:mysql -u root没有后面的-p,-u是指用户,-p是指通过密码登录,然后就会提示输入密码。由于安全起见,刚开始应修改密码为一个非空密码,也可以避免以后操作的出错,因为我遇到了好几个问题都是和空密码有联系的。root修改密码:

[root@localhost ~]# mysqladmin -u root -p password 123
Enter password: 

        将root的密码改为123,因为我是已经改过root密码的,所以这里要加上-p还得输入原来的密码。

        当然除了root还可以有其他的用户,mysql中用户管理是通过一张表mysql.user,其实这也就是一张数据库表,也是可以通过sql语句来操作的,新建用户有两种方式:

mysql> select user, password from mysql.user;      
+------+-------------------------------------------+
| user | password                                  |
+------+-------------------------------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | root                                      |
| root | root                                      |
| user | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> grant all on *.* to "test"@"localhost" identified by "123";
Query OK, 0 rows affected (0.00 sec)

mysql> insert into mysql.user(Host, User, Password) values("localhost", "sqluser", password("123"))ser", password("123"));
Query OK, 1 row affected, 3 warnings (0.05 sec)

mysql> select user, password from mysql.user;                                                       
+---------+-------------------------------------------+
| user    | password                                  |
+---------+-------------------------------------------+
| ro