本文目錄一覽:
怎樣在Linux環境下安裝部署MySQL資料庫系統
在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=
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環境下安裝軟體的基本思路及方法。
linux 怎麼部署mysql資料庫
創建用於執行mysql服務程序的帳號:
[root@linuxprobe cmake-2.8.11.2]# cd ..
[root@linuxprobe src]# useradd mysql -s /sbin/nologin
創建資料庫程序和文件的目錄,並設置目錄的所屬與所組:
[root@linuxprobe src]# mkdir -p /usr/local/mysql/var
[root@linuxprobe src]# chown -Rf mysql:mysql /usr/local/mysql
安裝Mysql服務程序(解壓與編譯過程已省略):
[root@linuxprobe src]# tar xzvf mysql-5.6.19.tar.gz
[root@linuxprobe src]# cd mysql-5.6.19/
[root@linuxprobe mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc
[root@linuxprobe mysql-5.6.19]# make
[root@linuxprobe mysql-5.6.19]# make install
刪除系統默認的配置文件:
[root@linuxprobe mysql-5.6.19]# rm -rf /etc/my.cnf
生成系統資料庫(生成信息已省略):
[root@linuxprobe mysql-5.6.19]# cd /usr/local/mysql
[root@linuxprobe mysql]# ./scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/var
創建配置文件的軟連接文件:
[root@linuxprobe mysql]# ln -s my.cnf /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的資料
資料庫mysql怎麼放到伺服器上
有多種方法啊。介紹其中一種,使用mysql資料庫的ODBC驅動。
步驟:
1.安裝mysql資料庫的ODBC驅動,mysql-connector-odbc-3.51.23-win32.msi(其中*是版本號),下載並安裝。
2.在Mysql中創建資料庫實例。
3.打開控制面板 — 管理工具 — 數據源ODBC,在用戶DSN中添加一個MySQL ODBC 3.51數據源。
4.在登錄login選項卡中輸入數據源名稱Data Source Name,此處輸入MysqlDNS(也可以自己隨便命名,只要在後面導入數據的時候選擇正確的數據源名字就行);然後輸入伺服器Server,用戶User,密碼Password,輸入正確後選擇要導入的資料庫,Database選擇你需要導入的資料庫。在連接選項connect options中根據需要設置MySql使用的埠port和字符集Character Set。
註:字符集一定要和Mysql伺服器相對應,如果Mysql使用了gbk字符集,則一定要設置字符集為gbk,否則導入到Sql Server可能會出現問號亂碼。
5.打開sql server企業管理器,選擇該資料庫,單擊右鍵選擇所有任務 — 導出數據。
6.『選擇數據源』為默認,『選擇目的』為剛剛安裝的mySQL數據源,用戶/系統DSN為MysqlDNS。
方法2:
有多種方法啊。介紹其中一種,使用mysql資料庫的ODBC驅動。步驟:
1.安裝mysql資料庫的ODBC驅動,mysql-connector-odbc-3.51.23-win32.msi(其中*是版本號),下載並安裝。
2.在Mysql中創建資料庫實例。
3.打開控制面板 — 管理工具 — 數據源ODBC,在用戶DSN中添加一個MySQL ODBC 3.51數據源。
4.在登錄login選項卡中輸入數據源名稱Data Source Name,此處輸入MysqlDNS(也可以自己隨便命名,只要在後面導入數據的時候選擇正確的數據源名字就行);然後輸入伺服器Server,用戶User,密碼Password,輸入正確後選擇要導入的資料庫,Database選擇你需要導入的資料庫。在連接選項connect options中根據需要設置MySql使用的埠port和字符集Character Set。
註:字符集一定要和Mysql伺服器相對應,如果Mysql使用了gbk字符集,則一定要設置字符集為gbk,否則導入到Sql Server可能會出現問號亂碼。
5.打開sql server企業管理器,選擇該資料庫,單擊右鍵選擇所有任務 — 導出數據。
6.『選擇數據源』為默認,『選擇目的』為剛剛安裝的mySQL數據源,用戶/系統DSN為MysqlDNS。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/291062.html