先进入MySQL Community Downloads(https://dev.mysql.com/downloads/),选择使用红色红框标记的菜单 MySQL Community Server
因为我们这里示范安装的是MySQL 5.7.38,所以进入之后点击“Archives”
接着按照以下步骤操作
由于在linux上下载比较快,所以,先使用yum安装wget
yum install wget
然后使用wget下载mysql文件包(通常都是下载到指定文件夹下,这里是在"/usr/local/"目录)
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
下载完成后解压mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#解压tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz#将mysql-5.7.38-linux-glibc2.12-x86_64重命名为mysqlmv mysql-5.7.38-linux-glibc2.12-x86_64 mysq
创建用户组和用户并修改权限
groupadd mysqluseradd -r -g mysql mysql
创建数据目录并赋予权限
#创建数据库目录mkdir -p /data/mysql#赋予数据库目录权限chown mysql:mysql -R /data/mysql
修改配置“/etc/my.cnf”
vim /etc/my.cnf
# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld]user=mysqlport = 33306sql-mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTIONcharacter_set_server=utf8mb4init_connect='SET NAMES utf8'explicit_defaults_for_timestamp=truelower_case_table_names = 1## Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M## Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin## Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Mbasedir=/usr/local/mysqldatadir=/data/mysqlsocket=/tmp/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0 [mysqld_safe]log-error=/data/mysql/mysql.errpid-file=/data/mysql/mysql.pid
初始化数据库
cd /usr/local/mysql/bin./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
初始化完成之后,输出的日志最后一行会有
root@localhost: PASSWORD
或者输入cat/data/mysql/mysql.err查看临时密码
先将mysql.server移动到/etc/init.d/mysql中
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
然后启动mysql,并查看mysql状态
#启动mysqlservice mysql start#查看mysql运行状态service mysql statusps -ef|grep mysql
然后登录mysql
#由于当前并没有全局设置mysql命令,所以先切换到/usr/local/mysql/bin/文件夹下./mysql -uroot -pPASSWORD
然后再执行下面的操作
SET PASSWORD = PASSWORD('newPassword');ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;FLUSH PRIVILEGES; use mysql;#使root用户可以在任何IP访问update user set host = '%' where user = 'root';#刷新FLUSH PRIVILEGES;
然后在重新登录;
也有许多小伙伴问什么在其他目录下使用mysql登录命令没有用,mysql命令没有找到,这是因为没有设置软连接,此时,先找到我们安装mysql的目录
然后输入一下命令建立软连接
ln -s /usr/local/mysql/bin/mysql /usr/bin/
然后在全局使用mysql登录命令,就可以成功使用了,如图
设置mysql开机启动
#设置mysql开机自启动chkconfig mysql on#取消mysql开机自启动chkconfig mysql off# 查看系统服务列表,以及每个服务的运行级别chkconfig --list#或 systemctl list-unit-file
还有一种专业的设置mysql开机自启动的方法,查看详情请点击