linux下安装mysql

安装的mysql版本是5.7

通过命令wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz来下载安装包。

[root@localhost mysql]# pwd
/opt/mysql
[root@localhost mysql]# 
[root@localhost mysql]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

解压文件到/usr/local/mysql目录下。

[root@localhost mysql]# tar zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
...
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# ls
mysql-5.7  mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

创建数据目录,并赋予用户权限

[root@localhost mysql]# mkdir data
[root@localhost mysql]# ls
data  mysql-5.7  mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

配置my.cnf文件,命令是vim /etc/my.cnf,按自己的实际情况更改。

[mysqld]
bind-address=0.0.0.0
datadir=/var/lib/mysql
port=3306
user=root
basedir=/usr/local/mysql/mysql-5.7
datadir=/usr/local/mysql/mysql-5.7/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/mysql-5.7/data/mysql.err
pid-file=/usr/local/mysql/mysql-5.7/data/mysql.pid
#charcater config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

初始化:进入mysql的bin目录中执行命令:./mysql --default-file=/etc/my.cnf --basedir=/usr/local/mysql/mysql-5.7 --datadir=/usr/local/mysql/mysql-5.7/data --user=root --initialize
执行完成后,使用命令cat /usr/local/mysql/mysql-5.7/data/mysql.err查看生成的随机密码

2020-05-10T10:28:56.015048Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-05-10T10:28:56.034386Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-05-10T10:28:56.088732Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 0d716d76-92a9-11ea-8676-000c291e98b0.
2020-05-10T10:28:56.111313Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-05-10T10:28:56.112107Z 1 [Note] A temporary password is generated for root@localhost: K&coO0#2-tyW ------随机的密码

将mysql.server复制到/etc/init.d/mysql中:cp mysql-5.7/support-files/mysql.server /etc/init.d/mysql,然后启动mysql

[root@localhost bin]# service mysql start
Starting MySQL. SUCCESS! 

使用上述生成的随机密码登录mysql,进行修改:
首先修改密码:

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

mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

再使mysql可以远程连接:

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> update user set host='%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;

此时,mysql已经可以远程连接了,注意虚拟机的防火墙状态。
可以将mysql设置成开机启动,主要命令如下:

[root@localhost data]# cp /usr/local/mysql/mysql-5.7/support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost data]# chmod +x /etc/init.d/mysqld 
[root@localhost data]# chkconfig --add mysqld
[root@localhost data]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

如上状态的mysql,就可以开机启动了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值