怎样在Linux环境下安装部署MySQL数据库系统
2个回答
2020-09-18 · 百度认证:北京中公教育科技官方账号
中公教育IT培训优就业
中公教育IT培训品牌,致力于培养互联网领域的高端人才,以学员就业为目的,优质就业为宗旨,是一家集Java开发、Web前端、UI/UE交互设计、大数据、软件测试等课程为一体的IT培训机构。
向TA提问
关注
展开全部
在Linux安装软件需要预先做好如下一些准备:准备好Linux操作系统如:CentOS7。配置好yum源。
完成上述准备后,就可以动手安装MySQL数据库了。主要安装步骤如下:
1. 禁用selinux
setenforce 0
2. 上传安装文件到Linux
3.解压rpm包
tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
4.安装软件
yum install mysql-community-{libs,client,common,server}-*.rpm
5.启动mysql数据库初始化
systemctl start mysqld
6.修改vi /etc/my.cnf
添加:
[mysqld]
#可以在表中录入中文
character-set-server=utf8 #
explicit-defaults-for-timestamp
# 禁用当前密码认证策略,可以使用简单密码(生产环境不适用)
validate_password=0
7.重启mysql服务
systemctl restart mysqld
8.找临时登录密码
grep -i "temporary password" /var/log/mysqld.log
9.连接MySQL数据库
mysql -uroot -p 输入临时密码
10.修改root用户登录密码为简单密码(生产环境不适用)
alter user root@localhost identified by '';
11.配置MYSQL_PS1环境变量
修改家目录下:.bash_profile文件,添加
export MYSQL_PS1="\u@\h[\d]>"
12.使新环境变量生效
source /root/.bash_profile
13.重新连接mysql验证
mysql -uroot -p
除了上述安装方式以外,可能在公司中会遇到安装指定版本的需求,那么如何安装指定版本的MySQL数据呢?这时我们可以采用下载指定版本安装包进行安装的方式,主要步骤如下,假设CentOS7 linux最小安装,已经配置好yum。首先检查是否安装numactl包
rpm -qa|grep numactl
yum install numactl-libs-* # 如果没有安装需要安装。检查是否安装libaio包
rpm -qa|grep libaio
yum install libaio-* # 如果没有安装需要安装
具体安装步骤如下:
* 禁用selinux
setenforce 0
* 上传安装文件到Linux
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
* 创建mysql用户组和用户
groupadd -g 27 -r mysql
#-r创建系统账户,-M 不创建用户家目录 -N 不创建和用户名一样的用户组
useradd -M -N -g mysql -r -s /bin/false -c "MySQL Server" -u 27 mysql
id mysql
* 上传安装包到root家目录
* 解压二进制文件到/usr/local
tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /usr/local
* 解压目录改名为mysql
cd /usr/local
ls -l
mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
* 环境变量中添加mysql/bin目录
vi /root/.bash_profile
修改PATH=/usr/local/mysql/bin:$PATH:$HOME/bin
添加 export MYSQL_PS1="\u@\h[\d]>"
source /root/.bash_profile
* 创建/usr/local/mysql/etc/my.cnf选项文件 (也可以使用默认的/etc/my.cnf选项文件)
mkdir -p /usr/local/mysql/etc
mkdir -p /usr/local/mysql/mysql-files
* 编辑选项文件my.cnf填写默认选项
vi /usr/local/mysql/etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
log-error=/usr/local/mysql/data/mysqld.err
pid-file=/usr/local/mysql/data/mysqld.pid
secure_file_priv=/usr/local/mysql/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
Explicit-defaults-for-timestamp
character-set-server=utf8
[mysql]
socket=/usr/local/mysql/data/mysql.sock
* 初始化数据目录
cd /usr/local/mysql
mkdir data
chmod 750 data
chown mysql:mysql data
* 初始化数据库
cd /usr/local/mysql
bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --initialize
* 使用systemd管理mysql
例如:systemctl {start|stop|restart|status} mysqld
cd /usr/lib/systemd/system
touch mysqld.service
chmod 644 mysqld.service
vi mysqld.service
# 添加以下内容
[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/data/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
以上内容中注意:The --pid-file option specified in the my.cnf configuration file is ignored by systemd.
默认:LimitNOFILE = 5000,如果连接数(max_connection)需要调大,可以将LimitNOFILE 设置为最大65535
* 创建mysql.conf文件
cd /usr/lib/tmpfiles.d
#Add a configuration file for the systemd tmpfiles feature. The file is named mysql.conf and is placed in /usr/lib/tmpfiles.d.
cd /usr/lib/tmpfiles.d
touch mysql.conf
chmod 644 mysql.conf
* mysql.conf添加内容
vi mysql.conf
添加以下语句:
d /usr/local/mysql/data 0750 mysql mysql -
* 使新添加的mysqld服务开机启动
systemctl enable mysqld.service
* 手动启动mysqld
systemctl start mysqld
systemctl status mysqld
* 获得mysql临时登录密码
cat /usr/local/mysql/data/mysqld.err | grep "temporary password"
* 客户端登录连接mysql服务器
mysql -uroot -p
输入临时密码
* 修改MySQL用户root@localhost密码
mysql> alter user root@localhost identified by ''; #此处为了方便设置为空密码
* 测试新密码连接MySQL服务
mysql -uroot -p
至此,我们就完成了在Linux环境下安装MySQL的任务。通过这两种方式我们可以体会到在Linux环境下安装软件的基本思路及方法。
完成上述准备后,就可以动手安装MySQL数据库了。主要安装步骤如下:
1. 禁用selinux
setenforce 0
2. 上传安装文件到Linux
3.解压rpm包
tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
4.安装软件
yum install mysql-community-{libs,client,common,server}-*.rpm
5.启动mysql数据库初始化
systemctl start mysqld
6.修改vi /etc/my.cnf
添加:
[mysqld]
#可以在表中录入中文
character-set-server=utf8 #
explicit-defaults-for-timestamp
# 禁用当前密码认证策略,可以使用简单密码(生产环境不适用)
validate_password=0
7.重启mysql服务
systemctl restart mysqld
8.找临时登录密码
grep -i "temporary password" /var/log/mysqld.log
9.连接MySQL数据库
mysql -uroot -p 输入临时密码
10.修改root用户登录密码为简单密码(生产环境不适用)
alter user root@localhost identified by '';
11.配置MYSQL_PS1环境变量
修改家目录下:.bash_profile文件,添加
export MYSQL_PS1="\u@\h[\d]>"
12.使新环境变量生效
source /root/.bash_profile
13.重新连接mysql验证
mysql -uroot -p
除了上述安装方式以外,可能在公司中会遇到安装指定版本的需求,那么如何安装指定版本的MySQL数据呢?这时我们可以采用下载指定版本安装包进行安装的方式,主要步骤如下,假设CentOS7 linux最小安装,已经配置好yum。首先检查是否安装numactl包
rpm -qa|grep numactl
yum install numactl-libs-* # 如果没有安装需要安装。检查是否安装libaio包
rpm -qa|grep libaio
yum install libaio-* # 如果没有安装需要安装
具体安装步骤如下:
* 禁用selinux
setenforce 0
* 上传安装文件到Linux
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
* 创建mysql用户组和用户
groupadd -g 27 -r mysql
#-r创建系统账户,-M 不创建用户家目录 -N 不创建和用户名一样的用户组
useradd -M -N -g mysql -r -s /bin/false -c "MySQL Server" -u 27 mysql
id mysql
* 上传安装包到root家目录
* 解压二进制文件到/usr/local
tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /usr/local
* 解压目录改名为mysql
cd /usr/local
ls -l
mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
* 环境变量中添加mysql/bin目录
vi /root/.bash_profile
修改PATH=/usr/local/mysql/bin:$PATH:$HOME/bin
添加 export MYSQL_PS1="\u@\h[\d]>"
source /root/.bash_profile
* 创建/usr/local/mysql/etc/my.cnf选项文件 (也可以使用默认的/etc/my.cnf选项文件)
mkdir -p /usr/local/mysql/etc
mkdir -p /usr/local/mysql/mysql-files
* 编辑选项文件my.cnf填写默认选项
vi /usr/local/mysql/etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
log-error=/usr/local/mysql/data/mysqld.err
pid-file=/usr/local/mysql/data/mysqld.pid
secure_file_priv=/usr/local/mysql/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
Explicit-defaults-for-timestamp
character-set-server=utf8
[mysql]
socket=/usr/local/mysql/data/mysql.sock
* 初始化数据目录
cd /usr/local/mysql
mkdir data
chmod 750 data
chown mysql:mysql data
* 初始化数据库
cd /usr/local/mysql
bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --initialize
* 使用systemd管理mysql
例如:systemctl {start|stop|restart|status} mysqld
cd /usr/lib/systemd/system
touch mysqld.service
chmod 644 mysqld.service
vi mysqld.service
# 添加以下内容
[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/data/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
以上内容中注意:The --pid-file option specified in the my.cnf configuration file is ignored by systemd.
默认:LimitNOFILE = 5000,如果连接数(max_connection)需要调大,可以将LimitNOFILE 设置为最大65535
* 创建mysql.conf文件
cd /usr/lib/tmpfiles.d
#Add a configuration file for the systemd tmpfiles feature. The file is named mysql.conf and is placed in /usr/lib/tmpfiles.d.
cd /usr/lib/tmpfiles.d
touch mysql.conf
chmod 644 mysql.conf
* mysql.conf添加内容
vi mysql.conf
添加以下语句:
d /usr/local/mysql/data 0750 mysql mysql -
* 使新添加的mysqld服务开机启动
systemctl enable mysqld.service
* 手动启动mysqld
systemctl start mysqld
systemctl status mysqld
* 获得mysql临时登录密码
cat /usr/local/mysql/data/mysqld.err | grep "temporary password"
* 客户端登录连接mysql服务器
mysql -uroot -p
输入临时密码
* 修改MySQL用户root@localhost密码
mysql> alter user root@localhost identified by ''; #此处为了方便设置为空密码
* 测试新密码连接MySQL服务
mysql -uroot -p
至此,我们就完成了在Linux环境下安装MySQL的任务。通过这两种方式我们可以体会到在Linux环境下安装软件的基本思路及方法。
2017-02-07
展开全部
创建用于执行mysql服务程序的帐号:
[root@linuxprobe cmake-2f
生成系统数据库(生成信息已省略):
[root@linuxprobe mysql-5f /etc/my.cnf
将mysqld服务程序添加到开机启动项:
[root@linuxprobe mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@linuxprobe mysql]# chmod 755 /etc/init.d/mysqld
[root@linuxprobe mysql]# chkconfig mysqld on
编辑启动项的配置文件:
[root@linuxprobe mysql]# vim /etc/rc.d/init.d/mysqld
//分别修改第46与47行,basedir为程序安装路径,datadir为数据库存放目录。
basedir=/usr/local/mysql
datadir=/usr/local/mysql/var
重启mysqld服务程序:
[root@localhost mysql]# service mysqld start
Starting MySQL. SUCCESS!
把mysql服务程序命令目录添加到环境变量中(永久生效):
[root@linuxprobe mysql]# vim /etc/profile
//在配置文件的最下面追加:
export PATH=$PATH:/usr/local/mysql/bin
[root@linuxprobe mysql]# source /etc/profile
将mysqld服务程序的库文件链接到默认的位置:
[root@linuxprobe mysql]# mkdir /var/lib/mysql
[root@linuxprobe mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@linuxprobe mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
[root@linuxprobe mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
初始化mysqld服务程序:
[root@linuxprobe mysql]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password: 输入要为root用户设置的数据库密码。
Re-enter new password: 重复再输入一次密码。
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y(删除匿名帐号)
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y(禁止root用户从远程登陆)
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y(删除test数据库并取消对其的访问权限)
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y(刷新授权表,让初始化后的设定立即生效)
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
可以百度搜索Linux就该这么学,第9章 使用Apache服务部署静态网站,里面有部署mysql的资料
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询