linux 安装mysql并设置密码

manjaro安装MySQL并设置密码:

安装MySQL:

sudo pacman -S mysql

修改配置文件:

sudo vim /etc/mysql/my.cnf  

末行添加:

skip-grant-tables

终端输入:

$ mysql
修改密码,把passwd替换成想要修改的密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'passwd';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

报错的解决方法:
mysql> flush privileges;

再次修改密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'passwd';
Query OK, 0 rows affected (0.03 sec)

刷新MySQL的系统权限相关表:
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

退出MySQL:
mysql> quit
Bye

注释最后一行

sudo vim /etc/mysql/my.cnf 
# skip-grant-tables  或者直接删除

重启mysql

sudo systemctl restart mysqld.service    

连接mysql测试一下是否成功:

~ >>> mysql -uroot -p                                                                                                                                                     
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.21 Source distribution

Copyright (c) 2000, 2020, 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> quit
Bye

Welcome to the MySQL monitor.代表已成功登录MySQL;

ubuntu20.04设置mysql8.0密码

刚安装完mysql时是默认没有密码的,终端直接输入mysql可以直接进入了

按照以下步骤输入:

1. ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
2. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
3. FLUSH PRIVILEGES;
4. alter user 'root'@'localhost' identified by '123456';重置密码用户名为root的密码

centos 安装mysql 并设置密码

安装mysql:
yum install mysql mysql-devel -y

查看是否安装成功:
yum list mysql-server

修改密码:

$ mysql

mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> FLUSH PRIVILEGES;
mysql> quit