Ubuntu 离线安装mariadb 10.4及mysql初始化

该文详细介绍了如何在没有网络连接的Ubuntu18.04系统上安装MariaDB10.4。首先在一台联网的Ubuntu系统中下载所需安装包,然后创建离线包并打包,接着将包传输到离线电脑,修改其sources.list,最后在离线电脑上安装并初始化数据库。
摘要由CSDN通过智能技术生成

准备工作

  • 需要一台能连互联网的ubuntu电脑
  • 操作系统版本与离线电脑版本一致,本文使用的是ubuntu18.04-server版本

联网电脑操作

  1. 安装dpkg-dev,后面要用到dpkg-scanpackages命令
apt-get install dpkg-dev
  1. 下载安装包,首先清空/var/cache/apt/archives目录,这里选择备份,防止文件丢失
    2.1 安装基础组件

    # 备份archives
    mv /var/cache/apt/archives /var/cache/apt/archives-bak
    # 新建一个archives目录
    mkdir /var/cache/apt/archives
    # 新建一个文件夹,后面用于拷贝
    mkdir /home/dorahou/mariadb/
    # 安装mariadb,此处不是真的安装,而是下载所有用到的离线包,-d表示只下载,是必加的
    # 如果是在线安装,则不需要加-d
    apt-get -d install software-properties-common
    apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
    

    2.2 安装指定版本数据库,mariadb10.4版本,所以要修改源

    vi /etc/apt/sources.list
    

    在sources.list第一行添加如下内容,注意保留其他在线链接

    deb [arch=amd64] https://mirrors.ustc.edu.cn/mariadb/repo/10.4/ubuntu bionic main
    

    2.3 下载安装包

    apt-get update
    apt-get -d install mariadb-server mariadb-client
    

    2.4 离线装包打包

    # 复制archives文件夹,并用dpkg-scanpackages建Packages.gz索引
    cp -r /var/cache/apt/archives /home/dorahou/mariadb/
    cd /home/dorahou/mariadb/
    dpkg-scanpackages archives /dev/null | gzip > archives/Packages.gz
    # 打包,并将your_software.tar.gz拷贝到离线电脑或服务器上
    cd ..
    tar czvf mariadb.tar.gz mariadb
    

离线电脑操作

  1. 将mariadb.tar.gz拷贝到电脑或服务器上,并解压,也可以直接下载我已制作的包mariadb离线安装包
cd /home/dorahou/
tar xzvf mariadb.tar.gz
pwd
  1. 创建本地源
    备份原始文件
# 备份源文件
mv  /etc/apt/sources.list  /etc/apt/sources.list-bak

编辑sources.list,在里面添加

# 编辑sources.list,在里面添加
deb [trusted=yes] file:/home/dorahou/mariadb archives/

安装mariadb-server

# 安装 mariadb
apt-get update
apt-get install mariadb-server
systemctl start mariadb

mysql 初始化

mariadb -v
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, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't 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
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y
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
 ... 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 you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

数据库使用

mysql -u root -p
# 修改端口,取消本地绑定
vi /etc/mysql/my.cnf

my.cnf参考文档

# MariaDB database server configuration file.
#
# You can copy this file to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
# add by dorahou
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
symbolic-links=0

lower_case_table_names=1
#max_allowed_packet=30M
slow_query_log=on
slow_query_log_file=/home/dorahou/mysql_data/slow_query_log.log
long_query_time=2


#
# * Basic Settings
#
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 33066
basedir		= /usr
datadir		= /home/dorahou/mysql_data/mysql
tmpdir		= /tmp
lc_messages_dir	= /usr/share/mysql
lc_messages	= en_US
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address		= 127.0.0.1
#
# * Fine Tuning
#
max_connections		= 100
connect_timeout		= 5
wait_timeout		= 600
max_allowed_packet	= 30M
thread_cache_size       = 128
sort_buffer_size	= 4M
bulk_insert_buffer_size	= 16M
tmp_table_size		= 32M
max_heap_table_size	= 32M
#
# * MyISAM
#
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched. On error, make copy and try a repair.
myisam_recover_options = BACKUP
key_buffer_size		= 128M
#open-files-limit	= 2000
table_open_cache	= 400
myisam_sort_buffer_size	= 512M
concurrent_insert	= 2
read_buffer_size	= 2M
read_rnd_buffer_size	= 1M
#
# * Query Cache Configuration
#
# Cache only tiny result sets, so we can fit more in the query cache.
query_cache_limit		= 128K
query_cache_size		= 64M
# for more write intensive setups, set to DEMAND or OFF
#query_cache_type		= DEMAND
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
#
# we do want to know about network errors and such
log_warnings		= 2
#
# Enable the slow query log to see queries with especially long duration
#slow_query_log[={0|1}]
#slow_query_log_file	= /var/log/mysql/mariadb-slow.log
#long_query_time = 10
#log_slow_rate_limit	= 1000
log_slow_verbosity	= query_plan

#log-queries-not-using-indexes
#log_slow_admin_statements
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id		= 1
#report_host		= master1
#auto_increment_increment = 2
#auto_increment_offset	= 1
log_bin			= /var/log/mysql/mariadb-bin
log_bin_index		= /var/log/mysql/mariadb-bin.index
# not fab for performance, but safer
#sync_binlog		= 1
expire_logs_days	= 10
max_binlog_size         = 100M
# slaves
#relay_log		= /var/log/mysql/relay-bin
#relay_log_index	= /var/log/mysql/relay-bin.index
#relay_log_info_file	= /var/log/mysql/relay-bin.info
#log_slave_updates
#read_only
#
# If applications support it, this stricter sql_mode prevents some
# mistakes like inserting invalid dates etc.
#sql_mode		= NO_ENGINE_SUBSTITUTION,TRADITIONAL
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
default_storage_engine	= InnoDB
innodb_buffer_pool_size	= 256M
innodb_log_buffer_size	= 8M
innodb_file_per_table	= 1
innodb_open_files	= 400
innodb_io_capacity	= 400
innodb_flush_method	= O_DIRECT
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

[mysqldump]
quick
quote-names
max_allowed_packet	= 16M

[mysql]
#no-auto-rehash	# faster start of mysql but no tab completion
default-character-set=utf8

[isamchk]
key_buffer		= 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!include /etc/mysql/mariadb.cnf
!includedir /etc/mysql/conf.d/

创建数据库

systemctl restart mariadb
mysql -u root -P 33066 -p
set PASSWORD=PASSWORD("123456");
# 退出后重新登录
# 依次执行以下命令建立数据库、用户、授权
create database dorahou_db character set utf8;
GRANT USAGE ON *.* TO 'dorahou'@'%' IDENTIFIED BY 'your_password'  WITH GRANT OPTION;
GRANT all privileges on smartvi .* to 'dorahou'@'%' identified by 'your_password';
flush privileges;

导入表结构
mysql -u dorahou -p dorahou_db< dorahou_db.sql

如有中文乱码问题可用如下命令查看
show variables like “%character%”;

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 要在Linux上离线安装MariaDB,您需要按照以下步骤操作: 1.下载MariaDB二进制文件:您可以从MariaDB官方网站下载适用于您的Linux发行版的二进制文件。 2.将二进制文件复制到Linux服务器:将下载的二进制文件复制到您的Linux服务器上。 3.解压缩二进制文件:使用tar命令解压缩二进制文件。 4.创建MariaDB数据目录:使用mkdir命令创建一个新的目录,用于存储MariaDB数据。 5.初始化MariaDB数据库:使用mysql_install_db命令初始化MariaDB数据库。 6.启动MariaDB服务:使用systemctl命令启动MariaDB服务。 7.配置MariaDB:使用mysql_secure_installation命令配置MariaDB,包括设置root密码和删除测试数据库等。 8.测试MariaDB:使用mysql命令测试MariaDB是否正常工作。 以上是在Linux上离线安装MariaDB的基本步骤。请注意,具体步骤可能因Linux发行版而异。 ### 回答2: MariaDB是一个免费的、开源的关系型数据库管理系统,它是MySQL的一个分支。在Linux操作系统上,安装MariaDB有多种方法,本文将介绍如何离线安装MariaDB。 一、下载MariaDB安装包 在安装MariaDB之前,我们需要下载安装包。可以在官方网站(https://mariadb.org/download/)上下载最新的稳定版本的MariaDB的tar.gz压缩包。在下载页面中,有多个选项,可以根据自己的需要选择对应的版本。在本次安装中,我们选择了10.3.17版本的MariaDB。 二、解压安装包 下载完成后,我们进入下载目录,在终端中输入以下命令进行解压安装包: $ tar -zxvf mariadb-10.3.17.tar.gz 此命令将解压后的文件放置在当前目录下的“mariadb-10.3.17”文件夹中。 三、创建用户与组 接下来,我们需要创建一个MariaDB用户和一个MariaDB组,用来运行MariaDB服务。在终端中输入以下命令: $ sudo groupadd mysql $ sudo useradd -g mysql mysql 四、安装依赖库 在离线安装中,我们需要手动安装MariaDB所依赖的库。在终端中输入以下命令: $ sudo yum install libaio $ sudo yum install libaio-devel $ sudo yum install ncurses-devel 五、配置MariaDB 接下来,我们需要进入到MariaDB文件夹中,对其进行配置。在终端中输入以下命令: $ cd mariadb-10.3.17 $ sudo cmake . –DCMAKE_INSTALL_PREFIX=/usr/local/mysql –DMYSQL_DATADIR=/usr/local/mysql/data –DSYSCONFDIR=/etc –DWITH_INNOBASE_STORAGE_ENGINE=1 –DWITH_MYISAM_STORAGE_ENGINE=1 –DWITH_EMBEDDED_SERVER=1 –DENABLED_LOCAL_INFILE=1 注:其中,-DCMAKE_INSTALL_PREFIX表示安装目录,-DMYSQL_DATADIR表示数据库文件存放目录,-DSYSCONFDIR表示配置文件存放目录,以上路径可自行更改 六、编译并安装MariaDB 配置完成后,我们需要编译并安装MariaDB。在终端中输入以下命令: $ sudo make $ sudo make install 安装完成后,我们需要对MariaDB进行初始化。在终端中输入以下命令: $ sudo /usr/local/mysql/scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data 接下来,我们需要将MariaDB的安装目录添加到系统环境变量中。在终端中输入以下命令: $ sudo vi /etc/profile 在文件末尾加入以下代码: export PATH=$PATH:/usr/local/mysql/bin 保存并退出。在终端中输入以下命令使配置生效: $ source /etc/profile 七、启动MariaDB 现在,我们可以启动MariaDB服务,使用以下命令: $ sudo /usr/local/mysql/support-files/mysql.server start 使用以下命令查看MariaDB服务状态: $ sudo /usr/local/mysql/support-files/mysql.server status 八、测试连接 现在,我们可以测试连接到MariaDB。在终端中输入以下命令: $ mysql -u root -p 此命令将提示输入root用户的密码,输入密码后,将会进入MariaDB的命令行模式。 至此,MariaDB离线安装完成。通过以上步骤,我们可以在Linux系统中顺利地安装MariaDB,为后续的数据库操作提供基础支持。 ### 回答3: MariaDB是一个开源的关系型数据库管理系统,是MySQL项目的社区分支。相比于MySQLMariaDB更加稳定,性能更优秀,使用也更加方便。本文将详细介绍如何在Linux系统中离线安装MariaDB。 1. 下载MariaDB 首先,需要下载MariaDB的二进制安装包。可以从官方网站 https://downloads.mariadb.org/ 下载安装包,选择适合自己系统的版本和对应的版本号,下载到本地。 2. 安装依赖包 在安装MariaDB之前,需要确保系统已经安装了以下依赖包: - zlib-devel: 用于压缩和解压缩数据的库。 - openssl-devel: 用于支持SSL连接的库。 - ncurses-devel: 用于支持终端界面的库。 如果没有安装上述依赖包,可以在命令行中使用以下命令进行安装: ``` yum install zlib-devel openssl-devel ncurses-devel -y ``` 3. 安装MariaDB 在下载了MariaDB的二进制安装包和安装了依赖包之后,可以进行MariaDB的安装。在命令行中执行以下命令: ``` tar -xzvf mariadb-10.3.9-linux-x86_64.tar.gz cd mariadb-10.3.9-linux-x86_64/ ./bin/mysql_install_db --user=mysql --basedir=/opt/mariadb --datadir=/opt/mariadb/data ``` 执行完上述命令之后,会将MariaDB安装到/opt/mariadb目录下,并初始化数据目录/opt/mariadb/data。 4. 修改配置文件 MariaDB的配置文件为my.cnf,需要根据实际需要进行修改。可以将my.cnf复制到/opt/mariadb/etc/目录下,并进行修改。 以下是一个简单的my.cnf配置文件: ``` [client] port = 3306 socket = /opt/mariadb/data/mysql.sock [mysqld] port = 3306 socket = /opt/mariadb/data/mysql.sock pid-file = /opt/mariadb/data/mysql.pid character-set-server = utf8mb4 collation-server = utf8mb4_general_ci datadir = /opt/mariadb/data log-error = /opt/mariadb/logs/mysqld.log log-bin = /opt/mariadb/logs/mariadb-bin expire_logs_days = 14 max_binlog_files = 100 max_connections = 300 query_cache_size = 256M innodb_buffer_pool_size = 2G innodb_flush_log_at_trx_commit = 2 innodb_flush_method = O_DIRECT [safe_mysqld] err-log = /opt/mariadb/logs/mysqld_safe.log ``` 5. 启动MariaDB 在完成了依赖包安装、MariaDB安装和my.cnf配置之后,可以启动MariaDB。在命令行中执行以下命令: ``` /opt/mariadb/bin/mysqld_safe --defaults-file=/opt/mariadb/etc/my.cnf & ``` 启动成功后,可以通过命令行或其他客户端工具连接到MariaDB。默认的用户名为root,密码为空。 6. 添加MariaDB到系统服务 为了方便启动和关闭MariaDB,可以将MariaDB添加到系统服务中。可以创建一个mariadb.service文件,将其保存到/etc/systemd/system/目录下。 以下是mariadb.service文件的内容: ``` [Unit] Description=MariaDB database server After=network.target [Service] Type=simple User=root WorkingDirectory=/opt/mariadb PIDFile=/opt/mariadb/data/mysql.pid ExecStart=/opt/mariadb/bin/mysqld_start ExecStop=/opt/mariadb/bin/mysqld_stop [Install] WantedBy=multi-user.target ``` 保存后,执行以下命令使mariadb.service生效: ``` systemctl daemon-reload systemctl enable mariadb.service ``` 现在,可以使用以下命令管理MariaDB: ``` systemctl start mariadb.service # 启动MariaDB服务 systemctl stop mariadb.service # 停止MariaDB服务 systemctl status mariadb.service # 查看MariaDB服务状态 systemctl restart mariadb.service # 重启MariaDB服务 ``` 至此,Linux系统中离线安装MariaDB的过程就介绍完毕了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值