//编辑mysql配置文件 [root@mysql local]# vim /etc/ld.so.conf.d/mysql.conf //在里面写入 /usr/local/mysql/lib
[root@mysql local]# ldconfig //执行一下看有无报错信息
[root@mysql local]# vim /etc/my.cnf //在其中写入已下信息 [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve
[root@mysql local]# vim mysql/support-files/mysql.server //在其中修改 # If you change base dir, you must also change datadir. These may get # overwritten by settings in the MySQL configuration files.
//设置数据库密码,root是数据库用户root,不是系统的管理员用户 [root@mysql local]# /usr/local/mysql/bin/mysql -u root -e "set password=password('redhat')" [root@mysql local]# /usr/local/mysql/bin/mysql -uroot -predhat mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> //这就可以登陆了
//配置mysql环境变量 [root@mysql local]# vim /etc/profile //在其最末尾写入 export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
[root@mysql local]# source /etc/profile //此时就可以直接用mysql命令了 [root@mysql local]# mysql -uroot -predhat mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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.
操作符 >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 条件逻辑操作 AND
mysql> select * from student where id<=3; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | tom | 20 | | 2 | marry | 20 | | 3 | rose | 21 | +----+-------+------+ 3 rows in set (0.00 sec)
mysql> select * from student where name='lisi'; +----+------+------+ | id | name | age | +----+------+------+ | 5 | lisi | 21 | +----+------+------+ 1 row in set (0.01 sec)
mysql> select * from student where age<=20; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | tom | 20 | | 2 | marry | 20 | +----+-------+------+ 2 rows in set (0.00 sec)
//创建名为qin的数据库用户并设置密码redhat。 mysql> create user 'qin'@'192.168.100.111' identified by 'redhat'; Query OK, 0 rows affected (0.01 sec)
//exit;退出后使用qin用户登陆 [root@mysql ~]# mysql -u qin -h 192.168.100.111 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 | +--------------------+ 1 row in set (0.00 sec) //普通用户的权限特别小
删除数据库用户
语法:drop user + ’用户名‘ + @ + ’主机ip‘ //这里我们使用root用户登陆
1 2 3
mysql> drop user 'qin'@'192.168.100.111'; Query OK, 0 rows affected (0.00 sec)
[root@mysql ~]# mysql -uroot -predhat //先使用root用户登陆 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 user 'shiqian'@'%' identified by 'redhat'; //创建shiqian用户 Query OK, 0 rows affected (0.01 sec)
mysql> grant select on sq.student to 'shiqian'@'%'; //给shiqian用户赋予select权限 Query OK, 0 rows affected (0.00 sec)
//验证 mysql> exit; //退出root用户登陆 Bye [root@mysql ~]# mysql -ushiqian -predhat //使用shiqian用户登陆 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> use sq //进入sq数据库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> select * from student; //查看student表 +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | tom | 20 | | 2 | marry | 20 | | 3 | rose | 21 | | 4 | zhangsan | 21 | | 5 | lisi | 21 | +----+----------+------+ 5 rows in set (0.00 sec)
// 查看shiqian用户的权限 mysql> show grants; +-------------------------------------------------+ | Grants for shiqian@% | +-------------------------------------------------+ | GRANT USAGE ON *.* TO 'shiqian'@'%' | | GRANT SELECT ON `sq`.`student` TO 'shiqian'@'%' | +-------------------------------------------------+ 2 rows in set (0.00 sec)
//查看root用户权限 mysql> show grants; +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 2 rows in set (0.00 sec)
mysql> show grants for 'shiqian'@'%'; +-------------------------------------------------+ | Grants for shiqian@% | +-------------------------------------------------+ | GRANT USAGE ON *.* TO 'shiqian'@'%' | | GRANT SELECT ON `sq`.`student` TO 'shiqian'@'%' | +-------------------------------------------------+ 2 rows in set (0.00 sec) //能看到shiqian用户现在有两条权限
mysql> revoke all on sq.student from 'shiqian'@'%'; Query OK, 0 rows affected (0.00 sec) //这里将shiqian用户的对sq下student表的select权限给撤销
mysql> show grants for 'shiqian'@'%'; +-------------------------------------+ | Grants for shiqian@% | +-------------------------------------+ | GRANT USAGE ON *.* TO 'shiqian'@'%' | +-------------------------------------+ 1 row in set (0.00 sec) //这里就只剩一条权限了