MySQL基础操作
一.登陆
1.数据库设置密码(初始默认没有密码)
[root@db03 ~]# mysqladmin -uroot password 123
2.使用密码登陆
#不规范登陆,密码显示在屏幕上 [root@db03 ~]# mysql -uroot -p123 #比较规范的登陆,交互式登陆 [root@db03 ~]# mysql -uroot -p Enter password: #错误登陆 [root@db03 ~]# mysql -uroot -p 123 Enter password: ERROR 1049 (42000): Unknown database '123' ##-p后面要直接接密码或者直接不加,不然会把123 当成一个库
二.操作数据库
1)库操作
1.查看库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
2.创建一个新的库
mysql> create database php; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | php | | test | +--------------------+ 5 rows in set (0.00 sec)
3.删除指定的库
mysql> drop database php; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.01 sec)
2)用户操作
1.查看用户
mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | db03 | | root | db03 | | | localhost | | root | localhost | +------+-----------+ 6 rows in set (0.00 sec)
2.删除指定用户
mysql> drop user root@'::1'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 172.0.0.1 | | | db03 | | root | db03 | | | localhost | | root | localhost | +------+-----------+ 5 rows in set (0.00 sec)
#root ::1 是ipv6的用户,用不到所以删了没关系,root localhost是绝对不能删的
3.创建新的用户
mysql> grant all on php.* to php@'172.16.1.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+------------+ | user | host | +------+------------+ | root | 172.0.0.1 | | php | 172.16.1.% | | | db03 | | root | db03 | | | localhost | | root | localhost | +------+------------+ 6 rows in set (0.00 sec) grant: 授权 all: 所有权限 on: php.* : 以php开头的文件 to: php: 用户名 @: 分隔符 '172.16.1.%': 允许这个网段的IP登陆这个用户,localhost则是只允许本机登陆 '123456': 密码
#例
mysql> grant all on *.* to nginx@'%' identified by 'nginx';
创建一个nginx用户,对所有文件都有所有权限,允许任意ip登陆,密码是 'nginx'
三.误删除所有用户
手抖删除了mysql所有用户
mysql> delete from mysql.user where 1=1; Query OK, 5 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
Empty set (0.00 sec)
#删除用户以后还是可以登陆,但是不要重启
1)解决方式一
1.停止数据库
[root@db03 ~]# systemctl stop mysqld
2.跳过授权表和网络启动
[root@db03 ~]# mysqld_safe --skip-grant-tables --skip-networking & [1] 37955 [root@db03 ~]# 200710 09:16:26 mysqld_safe Logging to '/usr/local/mysql/data/db03.err'. 200710 09:16:26 mysqld_safe A mysqld process already exists 回车 [1]+ Exit 1 mysqld_safe --skip-grant-tables --skip-networkin #登陆数据库 [root@db03 ~]# mysql -uroot -p Enter password:
3.插入新用户
insert into mysql.user values ('localhost','root',PASSWORD('123'), 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '',0,0,0,0,'mysql_native_password','','N');
4.查看用户的权限
mysql> select * from mysql.user where user='root'\G; *************************** 1. row *************************** Host: localhost User: root Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Reload_priv: Y Shutdown_priv: Y Process_priv: Y File_priv: Y Grant_priv: Y References_priv: Y Index_priv: Y Alter_priv: Y Show_db_priv: Y Super_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Execute_priv: Y Repl_slave_priv: Y Repl_client_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Create_user_priv: Y Event_priv: Y Trigger_priv: Y Create_tablespace_priv: Y ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: password_expired: N 1 row in set (0.00 sec)
5.重启数据库
[root@db03 ~]# mysqladmin shutdown 200710 09:32:01 mysqld_safe mysqld from pid file /usr/local/mysql/data/db02.pid ended [1]+ Done mysqld_safe --skip-grant-tables --skip-networking
[root@db03 ~]# systemctl start mysqld
2)解决方式二
1.停止数据库
[root@db03 ~]# systemctl stop mysqld
2.跳过授权表和网络启动
root@db03 ~]# mysqld_safe --skip-grant-tables --skip-networking & [1] 43983 [root@db02 ~]# 200710 09:38:49 mysqld_safe Logging to '/usr/local/mysql/data/db02.err'. 200710 09:38:49 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data [root@db03 ~]# mysql -uroot -p Enter password:
3.授权一个新用户
#刷新权限相关的表 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #创建root用户 mysql> grant all on *.* to root@'localhost' identified by '123' with grant option; Query OK, 0 rows affected (0.00 sec)
4.重启数据库
[root@db03 ~]# mysqladmin shutdown
200710 09:32:01 mysqld_safe mysqld from pid file /usr/local/mysql/data/db02.pid ended
[1]+ Done mysqld_safe --skip-grant-tables --skip-networking
[root@db03 ~]# systemctl start mysqld