Linux下安装Mysql
步骤:
一、下载Mysql的安装包
检查当前linux系统中是否已经安装了mysql或者mariadb
- 在安装mysql之前,我们需要先确保当前系统中,没有安装mysql或者mariadb,如果有需要先删除掉。
- 使用命令:
rpm -qa | grep mysql
和rpm -qa | grep mariadb
- 删除mariadb
- 使用命令:
yum remove mariadb-libs.x86_64
//删除mariadb数据库
[root@localhost bin]# yum remove mariadb-libs.x86_64
将mysql的安装包传到linux上
- 下载这个,然后使用xftp传到虚拟机中
- 或者使用weget命令直接下载到linux中
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
二、解压mysql8.0安装包
1. 解压安装包
- 在目录
/usr/local
目录下创建新的目录叫mysql
,mysql安装包解压之后都放到这个目录中
[root@localhost bin]# cd /usr/local
[root@localhost local]# mkdir mysql
- 进入目录
/opt/softwareJar
root@localhost softwareJar]# cd /opt/softwareJar
- 解压mysql安装包 ,解压后的文件都放在
/usr/local/mysql
目录中
[root@localhost softwareJar]# tar -xvJf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz -C /usr/local/mysql
2.文件重命名
- 将解压后的文件重命名,因为实在是太长了 ,改名为
mysql-8.0
[root@localhost mysql]# cd /usr/local/mysql
[root@localhost mysql]# mv mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz mysql-8.0
3.创建data存储目录
//进入这个解压之后的文件的目录
[root@localhost mysql-8.0]# cd /usr/local/mysql/mysql-8.0
//创建一个data目录,用于存放表的信息,还有其他的一些信息
[root@localhost mysql-8.0]# mkdir data
三、创建用户和用户组,并赋予权限
1.创建用户和用户组
//创建一个组叫mysql
groupadd mysql
//创建一个用户叫mysql,并加到mysql这个组中
useradd -g mysql mysql
2.给用户赋予权限
//设置/usr/local/mysql/mysql-8.0目录的所有者和所在组是mysql
chown -R mysql:mysql /usr/local/mysql/mysql-8.0
四、初始化mysql信息
1.切换到mysql8.0安装路径下的bin目录
[root@localhost bin]# cd /usr/local/mysql/mysql-8.0/bin
2.初始化mysql基本信息
basedir : mysql的安装目录
datadir : mysql中的data目录
[root@localhost bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql/mysql-8.0/data/ --initialize
3、获取到临时mysql登录的密码
记录下来这个密码
五、添加mysqld服务到系统
1.将mysqld服务添加到系统中
//进入mysql安装目录
[root@localhost mysql-8.0]# cd /usr/local/mysql/mysql-8.0
//将该目录下的support-files/mysql.server文件拷贝一份放到/etc/init.d/mysqld目录中
//放到/etc/init.d/目录下的就是添加的系统服务
[root@localhost mysql-8.0]# cp -a ./support-files/mysql.server /etc/init.d/mysqld
2.授权以及添加服务
[root@localhost mysql-8.0]# chmod +x /etc/init.d/mysqld
[root@localhost mysql-8.0]# chkconfig --add mysqld
3.创建mysql的配置文件my.cnf
- mysql5.7以后就没有my.cnf了,所以我们需要自己手动创建一个,将它创建在/etc/my.cnf这个地方,默认mysql启动的时候会来读取这个配置文件
[root@localhost etc]# cd etc
[root@localhost etc]# touch my.cnf
[root@localhost etc]# vim my.cnf
# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
default-character-set=utf8
#password = k0Ui&wV(Z3yt
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
server-id=101 #服务器唯一标识
#配置mysql的文件夹 和 mysql data目录
basedir=/usr/local/mysql/mysql-8.0
datadir=/usr/local/mysql/mysql-8.0/data
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Uncomment the following if you want to log updates
#log-bin=mysql-bin
# binary logging format - mixed recommended
#binlog_format=mixed
# Causes updates to non-transactional engines using statement format to be
# written directly to binary log. Before using this option make sure that
# there are no dependencies between transactional and non-transactional
# tables such as in the statement INSERT INTO t_myisam SELECT * FROM
# t_innodb; otherwise, slaves may diverge from the master.
#binlog_direct_non_transactional_updates=TRUE
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout
- 设置一下配置文件的权限
chmod 664 /etc/my.cnf
4.启动mysql服务
service mysqld start
如果上面的方法不行,使用下面的方法进行启动服务
systemctl start mysqld
5. 查看服务是否正常启动
ps -ef|grep mysql
6. 配置环境变量
- 使用vim编辑器编辑文件
/etc/profile
文件,这个文件中专门配置环境变量
- 配置完成之后,保存退出,使用命令重新加载一下该配置文件
source /etc/profile
六、登录mysql
1.登录使用开始随机生成的密码进行登录,账号为root
//可以在任意目录执行mysql命令,因为配置了环境变量
[root@localhost /]# mysql mysql -uroot -p
2.修改管理员密码 密码自定义设置
//设置root账号密码为123123
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123123';
3.重新加载缓存,使密码生效
mysql> flush privileges;
七、修改mysql配置,使其可以用工具远程登录
步骤:1.
mysql> update user set host='%' where user='root';
mysql> flush privileges;
- 开放3306端口,这样在windows中也可以访问到该数据库
//这是直接开启3306端口
[root@localhost bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
//重新加载一下防火墙
[root@localhost bin]# firewall-cmd --reload
//查看当前防火墙开放的端口号
[root@localhost bin]# firewall-cmd --zone=public --list-ports
这样远端可以通过linux虚拟机的ip地址,通过端口3306,还有linux系统中的root账号和对应密码来访问该数据库
八、退出登录,结束mysql服务
mysql> exit
Bye
//任意目录执行下面的这个命令,结束mysql服务
[root@localhost /]#mysqladmin -p 6379 shutdown
Enter password: