安装MySQL&mariadb

少年意气强不羁,虎胁插翼白日飞。这篇文章主要讲述安装MySQL&mariadb相关的知识,希望能为你提供帮助。
1、通过编译、二进制安装mysql5.7
二进制安装mysql-5.6.46```html/xml
mysql二进制安装,已经编译成二进制了,只需要做一些配置即可
[root@localhost ~]$ yum install autoconf libaio -y
[root@localhost ~]$ useradd -r -s /sbin/nologin -u 987 mysql#创建mysql用户
[root@localhost ~]$ mkdir /data/mysql
[root@localhost ~]$ chown mysql:mysql /data/mysql
[root@localhost ~]$ tar xvf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz -C /usr/local#指定解压路径
[root@localhost ~]$ cd /usr/local/
[root@localhost local]$ ln -s mysql-5.6.46-linux-glibc2.12-x86_64 mysql#二进制安装是编译过的,所以解压的路径必须是编译的路径;默认编译路径是/usr/local/mysql,所以对mysql解压包创建个软链接叫mysql
[root@localhost local]$ chown -R root:root mysql/#递归修改解压包的所有者和所属组
[root@localhost local]$ mkdir /etc/mysql
[root@localhost local]$ cp support-files/my-default.cnf /etc/mysql/my.cnf#复制mysql配置文件到指定目录;mysql配置文件可以放在多个目录;如:/etc/my.cnf、/etc/mysql/my.cnf等
[root@localhost local]$ vim /etc/mysql/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
datadir = /data/mysql#指定数据库数据存放路径
port = 3306
socket = /tmp/mysql.sock
innodb_file_per_table = on#设置存储引擎
skip_name_resolve = on#关闭DNS反向解析
skip-external-locking
[root@localhost local]$ cd /usr/local/mysql
[root@localhost mysql]$ scripts/mysql_install_db --datadir=/data/mysql --user=mysql#执行生成初始数据库的脚本,指定数据库文件所有者为mysql
[root@localhost mysql]$ cp support-files/mysql.server /etc/init.d/mysqld#复制启动脚本到指定目录;服务的启动脚本里面一般有chkconfig和description两行标识
[root@localhost mysql]$ chkconfig --add mysqld#把mysqld脚本加到开机启动列表里
[root@localhost mysql]$ echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh#设置PATH路径
[root@localhost mysql]$ . /etc/profile.d/mysql.sh#立即生效
[root@localhost mysql]$ mkdir /var/log/mariadb
[root@localhost mysql]$ touch /var/log/mariadb/mariadb.log
[root@localhost mysql]$ service mysqld start
[root@localhost mysql]$ mysql_secure_installation#执行安全加固脚本

#### 编译安装mysql-5.7.28 ```html/xml [root@localhost ~]$ yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel -y [root@localhost ~]$ useradd -s /sbin/nologin -r -u 987 mysql [root@localhost ~]$ mkdir /data/mysql#创建数据库数据存放目录 [root@localhost ~]$ chown mysql:mysql /data/mysql [root@localhost ~]$ tar xf mysql-5.7.28.tar.gz -C /usr/local/src/ [root@localhost ~]$ tar xf boost_1_59_0.tar.gz -C /usr/local/src#编译安装mysql-5.7.28需要依赖boost_1_59_0这个包,需要提前下好 [root@localhost ~]$ cd /usr/local/src/mysql-5.7.28 [root@centos7 mysql-5.7.28]# cmake . \\#编译mysql没有configure脚本,需要使用cmake,指定参数,生成Makefile文件 -DCMAKE_INSTALL_PREFIX=/app/mysql \\ -DMYSQL_DATADIR=/data/mysql/ \\ -DSYSCONFDIR=/etc/ \\ -DMYSQL_USER=mysql \\ -DWITH_INNOBASE_STORAGE_ENGINE=1 \\ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \\ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\ -DWITH_PARTITION_STORAGE_ENGINE=1 \\ -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \\ -DWITH_DEBUG=0 \\ -DWITH_READLINE=1 \\ -DWITH_SSL=system \\ -DWITH_ZLIB=system \\ -DWITH_LIBWRAP=0 \\ -DENABLED_LOCAL_INFILE=1 \\ -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \\ -DDEFAULT_CHARSET=utf8 \\ -DDEFAULT_COLLATION=utf8_general_ci \\ -DDOWNLOAD_BOOST=1 \\ -DWITH_BOOST=/usr/local/src/boost_1_59_0#指定boost所在目录 [root@localhost mysql-5.7.28]$ make & & make install [root@localhost mysql-5.7.28]$ cd /app/mysql/ [root@localhost mysql]$ bin/mysqld --initialize --datadir=/data/mysql --user=mysql#生成初始数据库,在最下面会给一个初始的root口令,需要记下来 [root@localhost mysql]$ echo PATH=/app/mysql/bin:$PATH > /etc/profile.d/mysql.sh#添加PATH路径 [root@localhost mysql]$ . /etc/profile.d/mysql.sh [root@localhost mysql]$ cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld#复制服务启动脚本 [root@localhost mysql]$ chkconfig --add mysqld#加入到开机启动列表里 [root@localhost mysql]$ vim /etc/my.cnf#编辑配置文件 [client] port=3306 socket=/data/mysql/mysql.sock [mysqld] port=3306 skip-external-locking datadir=/data/mysql socket=/data/mysql/mysql.sock [root@localhost mysql]$ service mysqld start [root@localhost mysql]$ mysql_secure_installation -p*nr!%iFl%7> i#执行安全加固脚本,密码为上面生成初始化数据库系统随机分配的密码如果在make编译时报错,排查完错误之后,需要先执行rm -f CMakeCache.txt操作,执行完之后再重新进行make编译

2、二进制安装mariadb10.4
安装libaio库文件```html/xml
yum install libaio* -y
##### 创建mariadb程序目录 官方编译时,默认指定的Mariadb的目录为:/usr/local/mysql. 我们可以把压缩包解压至:/usr/local/mysql,不过更通用的办法是建立一个软连接: ```html/xml tar -zxvfmariadb-10.4.10-linux-x86_64.tar.gz#解压mariadb包ln -s mariadb-10.4.10-linux-x86_64 /usr/local/mysql -r建立软连

初始化数据库```html/xml
mkdir /data/mariadb_data -p
chown root.root /data/mariadb_data -R
进入到mysql目录cd /usr/local/mysql,执行以下命令
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mariadb_data
Installing MariaDB/MySQL system tables in /data/mariadb_data ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system.....
--user 是用mysql用户运行
--basedir=是mysql程序的位置
--datadir=是打算让数据存放的位置
##### 关于mariadb的配置文件 在官方配置好的mariadb读取配置文件有顺序之分。 读取顺序:/etc/my.cnf > /etc/mysql/my.cnf > /usr/etc/my.cnf > ~/.my.cnf 一般来讲,前2个/etc/my.cnf后者/etc/mysql/my.cnf 比较常用,如果配置文件位置多了,反而不好管理,另外一种常用的配置就是在启动mysql时,给它指定好配置文件。 另外需要注意的是,有可能你的旧配置文件/etc/my.cnf存在,这样可能会影响到数据库的配置,此时需要保证该配置文件的配置有效性。 ```html/xml vim /etc/my.cnf[mysqld] datadir=/data/mariadb_data socket=/data/mariadb_data/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe] log-error=/data/mariadb_data/log/mariadb.log pid-file=/data/mariadb_data/pid/mariadb.pid# # include all files from the config directory # !includedir /etc/my.cnf.d

在/data/mysql下创建2个目录,用于保存日志和pid文件
```html/xml
mkdir /data/mariadb_data/log,pid
chown mysql.mysql -R /data/mariadb_data
##### 安全初始化 ```html/xml /usr/local/mysql/bin/mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE!PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, well need the current password for the root user. If youve just installed MariaDB, and havent set the root password yet, you should just press enter here.Enter current password for root (enter for none):#第一次运行直接回车 OK, successfully used password, moving on...Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation.You already have your root account protected, so you can safely answer n.Switch to unix_socket authentication [Y/n] n#您的根帐户已受保护,因此可以安全地回答“n”。 ... skipping.You already have your root account protected, so you can safely answer n.Change the root password? [Y/n] y#是否设置root密码 New password:#输入密码 Re-enter new password:#再次输入 Password updated successfully! Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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, MariaDB 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#是否删除测试数据库 - 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!Cleaning up...All done!If youve completed all of the above steps, your MariaDB installation should now be secure.Thanks for using MariaDB!注意:运行mysql_secure_installation有个小坑,官方在编译的时候,指定了它连接的socket文件是在/tmp目录下的,所以可以对socket文件做个软连接到/tmp目录下,这样可以解决找不到socket文件的问题。

启动mariadb【安装MySQL&mariadb】```html/xml
/usr/local/mysql/bin/mysqld_safe & #后台启动mariadb服务
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown#停止mariadb服务


    推荐阅读