安装的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,就可以开机启动了。