linux新安装mysql,在线安装

1.在root用户下,查看MySQL是否安装
[root@10-90-49-139-jhdxyjd ~]# rpm -qa|grep -i mysql   
mysql-community-common-5.7.26-1.el7.x86_64
mysql-community-client-5.7.26-1.el7.x86_64
mysql-community-server-5.7.26-1.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-libs-5.7.26-1.el7.x86_64
2.如果安装了MySQL,就先卸载,依次使用命令卸载

rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64

3安装MySQL服务器,需要联网

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

sudo yum install mysql-server

如果安装报错:
postfix-2.10.1-6.el7.x86_64需要:libmysqlclien 需要卸载

启动服务:sudo systemctl start mysqld

4.查看mysql服务是否启动陈宫:
[root@10-90-49-139-jhdxyjd ~]# ps -ef | grep mysql
mysql    63411     1  0 22:56 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     63464 63084  0 23:09 pts/0    00:00:00 grep --color=auto mysql

5.登录直接输入mysql,看是否需要初始密码

[root@10-90-49-156-jhdxyjd ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
 

解决方法,亲测有用

1.首先vi /etc/my.cnf,在最后添加skip-grant-tables  或者直接 echo “skip-grant-tables”  > /etc/my.cnf

2. 修改完后就可以直接登陆mysql不需要密码了。如下

[root@10-90-49-139-jhdxyjd ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> use mysql
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 user where user='root'
    -> ;
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| Host      | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin                | authentication_string                     | password_expired | password_last_changed | password_lifetime | account_locked |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| localhost | root | 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 | *DFA71C70471F2C45EA461E652007BCA032D8747F | Y                | 2021-04-24 22:46:25   |              NULL | N              |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
1 row in set (0.00 sec)

3.直接修改密码

 update user set authentication_string=PASSWORD("123456") where user='root';

注意上满修改的密码只能临时使用,再次进入mysql需要修改密码,否则报错。

SET PASSWORD=PASSWORD('123456');

注意:如果只想设置简单密码需要修改两个全局参数:
mysql>  set global validate_password_policy =0;
mysql> set global validate_password_length=1;

mysql> update user set authentication_string=PASSWORD("123456") where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

mysql> SET PASSWORD=PASSWORD('123456');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_policy =0;
Query OK, 0 rows affected (0.00 sec)

mysql>  set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql>  SET PASSWORD=PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye

4.修改vi /etc/my.cnf,删掉上面配置的skip-grant-tables

5.给mysql授权给其他用户使用

具体可以参考:mysql授权

 CREATE USER 'hadoop'@'%' IDENTIFIED BY '123456';
 
 GRANT ALL ON *.* TO 'hadoop'@'%';