1. 程式人生 > >centos上安裝mysql/mariadb安裝gogs

centos上安裝mysql/mariadb安裝gogs

tar.gz 創建 .tar.gz mysql- gogs star 查詢 root賬戶 fas

最小安裝:
1. 選擇English
2. DATE & TIME 修改好本地時間
   SOFTWARE SELECTION默認的Minimal Install就好
   INSTALLATION DESTINATION選一下
   然後,Begin Installation
3. ROOT PASSWORD設置ROOT密碼
   USER CREATION設置新用戶,勾選Make this user administrator
   等待安裝完成,點Reboot按鈕。



配置靜態IPv4:
$ systemctl status NetworkManager.service
檢查網絡管理器服務狀態

$ nmcli dev status
DEVICE  TYPE      STATE         CONNECTION
ens33   ethernet  disconnected  --
lo      loopback  unmanaged     --
這裏需要配置ens33

$ su
# cd /etc/sysconfig/network-scripts
# ls ifcfg*
# vi ifcfg-ens33
配置的ens33

將 BOOTPROTO=dhcp 改為 BOOTPROTO=static
然後在下行添加:
IPADDR=192.168.48.128
NETMASK=225.225.225.0
NM_CONTROLLED=no
將 ONBOOT=no 改為 ONBOOT=yes
保存退出vi(NM_CONTROLLED=no表示該接口通過該配置文件設置,而不是通過網絡管理器)

註:如果使用dhcp動態IP
只要將 ONBOOT=no 改為 ONBOOT=yes 即可。

# systemctl restart netowrk.service
# ip add
查看ip信息



配置光盤為安裝源(註:後面有配置iso為安裝源的,這裏僅光盤鏡像臨時作為安裝源使用):
$ sudo ssh 192.168.48.128 -l root
# cat /etc/yum.repos.d/CentOS-Media.repo
基本內容如下:
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
文件頭的註釋部分提示可以用命令
yum --enablerepo=c7-media [command]
或者
yum --disablerepo=\* --enablerepo=c7-media [command]
安裝軟件。

# ls /dev/cdrom
# mkdir /media/cdrom
# mount /dev/cdrom /media/cdrom
掛載cdrom


安裝vim、gcc、g++:
# yum --disablerepo=\* --enablerepo=c7-media install vim -y
# yum --disablerepo=\* --enablerepo=c7-media install gcc gcc-c++ -y


安裝mysql 5.6:
先將client和server兩個rpm安裝包復制到本機下載目錄,然後在mac終端輸入:
$ sudo scp ~/Downloads/MySQL-*.rpm [email protected]:/home/bob/
復制client和server安裝文件到/home/bob/目錄下(註:也可以復制到其他目錄,比如[email protected]:/usr/local/)
回到ssh:
# rpm -qa|grep -i mariadb
查詢mariadb,與mysql沖突,下面將查到的庫一並強制刪除(最小安裝只有mariadb-libs)
# rpm -e --nodeps mariadb-libs
# groupadd -r mysql
# useradd -g mysql mysql -d /home/mysql -s /sbin/nologin
(註:mysql 5.7論壇版安裝順序是:common、libs、client、server)
(註2:mariadb 10.2需要一些依賴:yum --disablerepo=\* --enablerepo=c7-media install lsof perl rsync boost perl-DBI -y
   然後安裝順序是:galera、MariaDB-10.2.10-centos73-x86_64-*(放在同一目錄下:compat、common、client、server)
 )
# yum --disablerepo=\* --enablerepo=c7-media install perl -y
# rpm -ivh ./MySQL-client-5.6.34-1.el7.x86_64.rpm
# yum --disablerepo=\* --enablerepo=c7-media install autoconf -y
# rpm -ivh ./MySQL-server-5.6.34-1.el7.x86_64.rpm
# rm -f MySQL-*.rpm
# systemctl start mysql.service
(註:mysql 5.7的啟動服務是mysqld.service)
(註2:mariadb 10.2啟動服務是mysqld.service,mariadb也可以)
# cat /root/.mysql_secret 
The random password set for the root user at Tue Feb 21 13:55:26 2017 (local time): vZQwDJLbxm_zHno_
(註:mysql 5.7的初始密碼在log文件裏
    $ grep "temporary password" /var/log/mysqld.log
    然後在調用mysql_secure_installation前,手動修改安全策略:
    $ mysql -uroot -p
    mysql> set global validate_password_policy=0;
    mysql> set global validate_password_length=0;
    mysql> exit
    或者編輯 /etc/my.cnf
    [mysqld]
    validate_password=off
)
(註2:mariadb 10.2沒有初始密碼,可以通過下面命令將密碼設置為123456
    /usr/bin/mysqladmin -u root password ‘123456‘
)
# /usr/bin/mysql_secure_installation
把隨機密碼粘貼到提示符,根據提示,改變root密碼,移除匿名賬戶,禁用root遠程登錄,移除測試數據庫,刷新權限。
(註:mysql 5.7在設置了前面的密碼策略後,可能要多次輸入簡單的密碼來確認)
# mysql -uroot -p
創建新用戶:
mysql> create user ‘bob‘@‘%‘ identified by ‘123456‘;
(如果用root賬戶遠程連接:mysql> grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘123456‘;)
mysql> flush privileges;
mysql> exit
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
------ mysql emoji ------
註意:utf8mb4的最低mysql版本支持版本為5.5.3+
如果創建數據庫、表、列時指定utf8mb4和utf8mb4_general_ci,下面應該可以不用設置。
1) 設置my.cnf
$ sudo vim /etc/my.cnf(ubuntu的位置 /etc/mysql/my.cnf)
[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect=‘SET NAMES utf8mb4‘

skip-name-resolve
保存後重啟mysql
$ systemctl restart mysql
$ mysql -uroot -p
mysql> SHOW VARIABLES WHERE Variable_name LIKE ‘character_set_%‘ OR Variable_name LIKE ‘collation%‘;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8mb4_general_ci         |
| collation_database       | utf8mb4_unicode_ci         |
| collation_server         | utf8mb4_unicode_ci         |
+--------------------------+----------------------------+
11 rows in set

註:collation_connection、collation_database、collation_server是utf8mb4_general_ci沒有關系。

2) 修改Database、Table、Column字符集:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE table_name CHANGE column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

修復優化表
REPAIR TABLE table_name;
OPTIMIZE TABLE table_name;
也可以用mysqlcheck命令優化所有數據庫
$ mysqlcheck -uroot -p --auto-repair --optimize --all-databases

3) 對於Java來說mysql connector版本需高於5.1.13。

安裝gogs:
先將linux_amd64.zip包復制到本機下載目錄,然後在mac終端輸入:
$ sudo scp ~/Downloads/linux_amd64.tar.gz [email protected]:/root/
復制linux_amd64.tar.gz文件到/root/目錄下(註:也可以復制到其他目錄,比如[email protected]:/usr/local/)
回到ssh:
# groupadd -r git
# useradd -g git git -d /home/git
# mv /root/linux_amd64.tar.gz /home/git/
# chown git:git /home/git/linux_amd64.tar.gz
(如果是zip文件:yum --disablerepo=\* --enablerepo=c7-media install unzip -y)
# su git
$ cd ~
$ tar xvfz linux_amd64.tar.gz
(如果是zip文件:unzip linux_amd64.zip)
$ ls
gogs  linux_amd64.tar.gz
$ rm -f linux_amd64.tar.gz
$ cd gogs
$ chmod +x ./gogs
$ cat ./scripts/mysql.sql
DROP DATABASE IF EXISTS gogs;
CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
$ mysql -uroot -p < scripts/mysql.sql
$ mysql -uroot -p
mysql> grant all privileges on gogs.* to ‘bob‘@‘%‘ identified by ‘123456‘;
mysql> flush privileges;
mysql> exit
$ exit
# cp /home/git/gogs/scripts/systemd/gogs.service /etc/systemd/system/
# cat /etc/systemd/system/gogs.service
[Unit]
Description=Gogs
After=syslog.target
After=network.target
After=mariadb.service mysqld.service postgresql.service memcached.service redis.service

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=simple
User=git
Group=git
WorkingDirectory=/home/git/gogs
ExecStart=/home/git/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target

啟動服務,配置防火墻
# systemctl start gogs.service
# systemctl enable gogs.service
# firewall-cmd --zone=public --add-port=3000/tcp --permanent
# firewall-cmd --reload
(ubuntu系統配置防火墻命令:
  sudo ufw allow 3000
  sudo ufw enable)

然後在瀏覽器打開
http://192.168.48.128:3000
(註2:mariadb配置好後,主機也許不能用127.0.0.1連接,請用主機ip:192.168.48.128)

註:如果開啟了selinux,可能需要關閉。
# vim /etc/sysconfig/selinux
將SELINUX=enforce改為disabled
需要重啟。


無法使用ssh進行clone和push數據
如果http方式沒問題,就是應為gogs的賬戶(這裏是git賬戶)沒有指定shell,
解決辦法是:
# cat /etc/shells
/bin/sh
/bin/bash
...
# usermod -s /bin/bash git


升級gogs
$ sudo service gogs stop
$ sudo su - git
$ cd ~
$ pwd
/home/git
$ ls
gogs gogs-repositories
$ mv gogs gogs_old

$ wget https://dl.gogs.io/gogs_v$VERSION_$OS_$ARCH.tar.gz
$ tar -zxvf gogs_v$VERSION_$OS_$ARCH.tar.gz
$ ls
gogs gogs_old gogs-repositories gogs_v$VERSION_$OS_$ARCH.tar.gz

$ cp -R gogs_old/custom gogs
$ cp -R gogs_old/data gogs
$ cp -R gogs_old/log gogs

$ sudo service gogs start

如果啟動失敗,可能是權限的問題:
# chmod +x /home/git/gogs/gogs
# chown -R git:git /home/git/gogs
# service gogs start

--------------------------

sudo
$ su
# vim /etc/sudoers
在 root  ALL=(ALL) ALL後面添加相同一行,新行的root改成當前用戶,然後w!寫入,退出vim
# exit
$ 
就可以了。


啟動到命令行界面
graphical.target to multi-user.target 
$ systemctl get-default 
graphical.target
$ sudo systemctl set-default multi-user.target 
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
$ sudo reboot
// boot to graphical desktop
$ startx


更改所有權
chown -R youruser:youruser /home/youruser/*


更換shell
# cat /etc/shells
# echo $SHELL
# usermod -s /sbin/nologin youruser
# usermod -s /sbin/bash youruser


設置iso作為安裝源
查看網址
# ip add
遠程拷貝
$ scp ~/Desktop/CentOS-7-x86_64-Everything-1611.iso [email protected]:/usr/local/
遠程登錄
$ ssh 192.168.48.128 -l root
移動文件到/usr/local/iso
# mkdir /usr/local/iso
# mv /usr/local/CentOS-7-x86_64-Everything-1611.iso /usr/local/iso/
修改fstab,掛著iso到/mnt/centiso
# vi /etc/fstab
追加:
/usr/local/iso/CentOS-7-x86_64-Everything-1611.iso /mnt/centiso iso9660 defaults,ro,loop 0 0
創建掛載點並掛載:
# mkdir /mnt/centiso
# mount -a
# ls /mnt/centiso
CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL
記住這裏面的GPG-KEY的文件,將用在yum配置源上
查看yum源
# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
新建一個cdrom.repo
# vi cdrom.repo
[CDROM]
name=centiso
baseurl=file:///mnt/centiso
enabled=1
gpgcheck=0
gkgkey=file///mnt/centiso/RPM-GPG-KEY-CentOS-7
檢查安裝源
# yum list gcc
Loaded plugins: fastestmirror
CDROM                                                    | 3.6 kB     00:00
base                                                     | 3.6 kB     00:00
...
可以看見CDROM安裝源
安裝vim
# yum install vim

centos上安裝mysql/mariadb安裝gogs